www.webdeveloper.com
Page 4 of 4 FirstFirst ... 234
Results 46 to 58 of 58

Thread: Interested in a challenge?

  1. #46
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    Validation is more elegant and most of the time faster with regular expressions
    HTML Code:
    <?xml version = "1.0"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns = "http://www.w3.org/1999/xhtml">
      <head>
        <script type="text/javascript">
          function validate(e) {
            if (!e.target)
              e.target = e.srcElement;
            switch(e.target.name) {
              case 'comments':
                var reg = /^[a-zA-Z\d]*$/m;
                break;
              case 'txtEmail':
                var reg = /^[^"']*$/;
                break;
              case 'txtPostalCode':
                var reg = /^\d$/;
                break;
              default:
                break;
            }
            alert(reg.test(e.target.value));
            if (reg.test(e.target.value))
              return true;
            else
              return false;
          }
        </script>
      </head>
      <body>
        <form onsubmit="return false;">
          This field will not accept special characters: (like !@#$%^&* etc) which is the same as only accepting a-z, A-Z and 0-9<br />
          <textarea rows="2" cols="20" name="comments" onkeyup="validate(event);" ></textarea><br /><br />
          This field will not accept double or single quotes:<br />
          <input type="text" name="txtEmail" onkeyup="validate(event);" /><br /><br />
          This field will only accept numbers:<br />
          <input type="text" name="txtPostalCode" onkeyup="validate(event);" />
        </form>
      </body>
    </html>
    In the first challenge some of you guys are discussing DOM methods vs. document.write/innerHTML. You should read this benchmark http://www.quirksmode.org/dom/innerhtml.html which shows that innerHTML is the superior approach.
    Last edited by Dok; 08-07-2007 at 01:56 PM.
    #define question (2B || !2B)
    HTMLElement and W3C Event Handling in IE
    My JavaScript Library

    Don't PM me about answers to questions. If I don't reply in a thread it's because:
    • You didn't read the message posting guidelines
    • Your code is too unstructured and/or formatted poorly - correcting it is too time consuming
    • I simply don't know the answer

  2. #47
    Join Date
    Mar 2005
    Posts
    767
    Your claims about what is more elegant or superior would be a bit more convincing if your code would actually work... also, tying the validation to the form elements' name is quite lame.
    Stop thinking, start drinking.

  3. #48
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    Your claims about what is more elegant or superior would be a bit more convincing if your code would actually work
    True - I meant no offence but it is working for me - what error are you getting? The superior part is not my conclusion but the article at quirksmode.
    tying the validation to the form elements' name is quite lame
    Yes but it is easier to illustrate which regex belongs to which input - passing the regex in the inline event handler often (and in this case) lead to runtime errors due to the regex syntax.
    #define question (2B || !2B)
    HTMLElement and W3C Event Handling in IE
    My JavaScript Library

    Don't PM me about answers to questions. If I don't reply in a thread it's because:
    • You didn't read the message posting guidelines
    • Your code is too unstructured and/or formatted poorly - correcting it is too time consuming
    • I simply don't know the answer

  4. #49
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    What about this tight simple regexp approach?:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    var r={
    'special':/[\W]/g,
    'quotes':/['\''&'\"']/g,
    'notnumbers':/[^\d]/g
    }
    function valid(o,w){
    o.value = o.value.replace(r[w],'');
    }
    </script>
    </head>
    <body>
    <form id="myform" method="" onsubmit="return false;">
    This field will not accept special characters: (like !@#$%^&* etc,) - but accepts underscore _
    <br>
    <textarea rows="2" cols="20" name="comments" onkeyup="valid(this,'special')" onblur="valid(this,'special')"></textarea>
    <br>
    <br>
    This field will not accept double or single quotes:
    <br>
    <input type="text" name="txtEmail" onkeyup="valid(this,'quotes')" onblur="valid(this,'quotes')">
    <br>
    <br>
    This field will only accept integer numbers:
    <br>
    <input type="text" name="txtPostal" onkeyup="valid(this,'notnumbers')" onblur="valid(this,'notnumbers')">
    </form>
    </body>
    </html>
    Last edited by Kor; 08-07-2007 at 04:20 PM.

  5. #50
    Join Date
    May 2004
    Location
    FL
    Posts
    3,447
    Ahhh, looks great, Kor. Nice and compact. What name and URL do you want me to use for credit?
    Lee

    ""Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."" -- Sir Winston Churchill

  6. #51
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by LeeU
    Ahhh, looks great, Kor. Nice and compact. What name and URL do you want me to use for credit?
    credit? What credit? I did it for fun. Nor did I use some alerts, which in fact is simple, on JSON way...

    I guess you need a paypal or smth... I have only 2 MasterCard cards... But... What is this all about?
    Last edited by Kor; 08-08-2007 at 03:11 PM.

  7. #52
    Join Date
    May 2004
    Location
    FL
    Posts
    3,447
    Well, I usually give credit to the author by using his name and a link to his Web site.
    Lee

    ""Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."" -- Sir Winston Churchill

  8. #53
    Join Date
    Mar 2005
    Posts
    767
    At the risk of sounding like a nitpicking old git in this thread, 'r' isn't a good choice for the name of a global variable in a script that could be mixed with others. Making the object a property of the function reduces the risk of namespace collisions:
    Code:
    function valid(o,w){
    o.value = o.value.replace(valid.r[w],'');
    }
    valid.r={
    'special':/[\W]/g,
    'quotes':/['\''&'\"']/g,
    'notnumbers':/[^\d]/g
    }
    Stop thinking, start drinking.

  9. #54
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    That's nice of you. I was afraid of money, you know.... . I guess you can use my full name : Corneliu Lucian "Kor" RUSU if you want... The mail, hmm... Even if I use most of the time gmail (public) and Thunderbird (work), I still keep aware of the spams&.

  10. #55
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Orc Scorcher
    At the risk of sounding like a nitpicking old git in this thread, 'r' isn't a good choice for the name of a global variable in a script that could be mixed with others. Making the object a property of the function reduces the risk of namespace collisions:
    Code:
    function valid(o,w){
    o.value = o.value.replace(valid.r[w],'');
    }
    valid.r={
    'special':/[\W]/g,
    'quotes':/['\''&'\"']/g,
    'notnumbers':/[^\d]/g
    }
    It was only a demo. Considering the fact that many use DW functions (which has a nasty habit to use single letter on it's variables globals) , you might be right. Let's name that object: regExP.... or whichever... Making it a local variable/object is not wise idea... if wrong managed, it could harm the "twin" global variable, or it could block the code. Anyway, a good point, I reckon...
    Last edited by Kor; 08-08-2007 at 03:51 PM.

  11. #56
    Join Date
    May 2004
    Location
    FL
    Posts
    3,447
    Quote Originally Posted by Kor
    That's nice of you. I was afraid of money, you know.... . I guess you can use my full name : Corneliu Lucian "Kor" RUSU if you want... The mail, hmm... Even if I use most of the time gmail (public) and Thunderbird (work), I still keep aware of the spams&.
    Not e-mail, a link to your Web site. (BTW, your last name is in all capitals?)
    Lee

    ""Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."" -- Sir Winston Churchill

  12. #57
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    just

    //Corneliu Lucian "Kor" Rusu corneliulucian[at]gmail[dot]com//

    would be enough

    No, my last name is not all in capitals. It's just a way to emphasize the last (family) name as, in different cultures, the family name may be placed first, you know...

  13. #58
    Join Date
    May 2004
    Location
    FL
    Posts
    3,447
    Gotcha, thanks! I'll be back later ...
    Lee

    ""Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."" -- Sir Winston Churchill

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles