Results 1 to 15 of 15

Thread: JS one (gulp) regular expression Q ...

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    JS one (gulp) regular expression Q ...

    Code:
    var blnRegExpValidEMail = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
    So here I am, just downloaded a working example of a JS e-mail validation code & pinching it's code to shove into my project
    Thought it best to do some comments for once & explain what on Earth I'm trying to do incase I come back to it later, so I'm swamped under my javascript books here trying to make some sense of what the above's trying!
    I've got :
    • "/^[a-zA-Z0-9._-]" Is checking for any character apart from those in the square brackets.
    • "+@" Checks that after the above characters have been removed, we've got 1 or more @ symbols in the string.

    Why I've got brackets in the next bit, or the "+\" bit I'm not 100% on, I've found that the $ is searching for a piece of text at the end of the string, buit again, not sure what the "{2,4}" bits for ?!?!?!?!?

    Can anyone help explain the "([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/" bit please ?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    cry's out loud

    Iv'e just done you a nice well commented reply, but then I was testing something and closed IE by mistake. *{5}

    Anyway that regular expression you had can be simplified into the one below, which will do the job of email validation no problem!

    Code:
    <script>
    
    //match (.+) one or more occurences of any character except a new line followed by @ (@) followed by (.+) one or more occurences of any charachter except a new line followed by a literal . (\.)
    var blnRegExpValidEMail = /.+@.+\./;
    
    var test = prompt("email please");
    
    var validate = test.match(blnRegExpValidEMail);
    
    if (validate != null){
    	alert("email valid");
    }else{
    	alert ("oh dear that was incorrect");	
    }
    
    </script>

  3. #3
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Hmmmm... this shouldn't be a problem. For really good RegEx help, there are dedicated books. I don't like the RegEx section in the Rhino.

    Code:
    /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/
    Okay, the caret (^) literally means, "match at begining of expression". So "^[a-zA-Z0-9._-]+" is looking for one or more characters at the begining of the string. It is looking for all lower and upper case roman characters and the 10 arabic numerals, in addition to looking for the underscore, the hyphen and ".", which translates to "any character except newline.".

    I would really drop the dot and replace most of that with "\w", which is equivalent to "a-zA-Z0-9_".

    Since it is trying to parse an email address, there should be a single at character. And that character should immediately follow the alphanumeric string at the beginning of the expression. The plus goes with the bracketed expression, not the @. It is looking for a single @.

    The next part, "([a-zA-Z0-9.-]+\.)+" is looking for one or more of alphanumeric chains with, each followed by a dot. So if your email address is [email protected], or [email protected], it will match. The paraenthesis group the stuff within, and it augemented by the plus. The plus means, "match the previous one or more times."

    I would widdle that expression down a bit, too.

    Now the last bit, "[a-zA-Z0-9.-]{2,4}$". The dollar sign means "match this only at the end of the string". The {2,4} mean "match the previous atleast 2 times, but no more than 4 times". It is looking for a to character or four character TLD at the end of the email address. Really, you can drop the numeric and hyphen search here, and just look for letters.

    Code:
    /^[\w-]+@([\w-]+\.)+[a-zA-Z]{2,4}$/
    I hope that makes sense. I tried to explain it all without boring you, but if I've left out anything, let me know.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  4. #4
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Took me so long to post, that someone beat me to it.

    But, Progressive, the following email address will break your expression thrice:

    Code:
    foo	[email protected]
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  5. #5
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    no it wont!

    so long as the @ is followed any character and then a fullstop it will be happy so it will gladly accept.

    [email protected]dom.t9dgibberish

    these are the only things you can be sure of in an email address it will contain a @ and a . so checking for anything else is pointless like checkking the number of occurences of characters and .

  6. #6

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    thanks guys, this helps a lot !

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  7. #7
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    no problem!

    regular expressions can be a pain but are extemely useful!

  8. #8
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Originally posted by progressive
    no it wont!

    so long as the @ is followed any character and then a fullstop it will be happy so it will gladly accept.

    [email protected]dom.t9dgibberish

    these are the only things you can be sure of in an email address it will contain a @ and a . so checking for anything else is pointless like checkking the number of occurences of characters and .
    Uhm... you are contaminating Alex's mind.

    First, the address I provided started at with an "f". It contained a tab character. Tabs are not allowed in email addresses. But, your RegEx happily matches tabs.

    Second, TLDs do not have numbers in them. But your RegEx happily matches "t9d".

    Third, the word "gibberish" was accidentally appended to an otherwise malformed address. Had it be appended to a legit address (such as [email protected]) it still would've been allowed to happily pass your RegEx.

    Your expression lets entirely too much stuff in. You might as well let everything in and do no matching at all.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  9. #9

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    You've warped my fragile little mind !

    Well I went with the one I had anyways, it don't matter too much but you both helped explain it to me (only got 1/2 of it from looking at the wrox books - left a chunk missing on this side of js )

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  10. #10
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    I somehow imagine "bob@bob/co.com" will pass the RegEx you had, even though it is not a valid email address.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  11. #11
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    I'll give credit where credits due. Your right I'm wrong CyberTHuG.

    At the end of the day though you can't stop a user from entering fraudulent email addresss. All you can do is make them think that you can, so checking for and @ symbol will make most people enter a correct address. When entering my email address in required fields i usually use.

    [email protected]

    hope thats not a real one as they'll be getting a lot of spam..lol

  12. #12
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    Originally posted by CiberTHuG

    Code:
    /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/
    Now the last bit, "[a-zA-Z0-9.-]{2,4}$". The dollar sign means "match this only at the end of the string". The {2,4} mean "match the previous atleast 2 times, but no more than 4 times". It is looking for a to character or four character TLD at the end of the email address. Really, you can drop the numeric and hyphen search here, and just look for letters.

    Code:
    /^[\w-]+@([\w-]+\.)+[a-zA-Z]{2,4}$/
    I hope that makes sense. I tried to explain it all without boring you, but if I've left out anything, let me know.
    There's a minor flaw in what follows the @ in the original regexp.
    The flaw would make @some_junk.com be illegal because _'s aren't included. Also, I like the CiberTHuG's [\w-] approach, but I'd include .'s too so: [\w-.] .

    alex_read thought that ^[blah] meant that everything except b, l, a, h was accepted. That's an understandable mistake because the ^ has multiple uses (as do most of regexp's symbols) depending on context. [^blah] would match any character but b, l, a, h.

  13. #13
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    According to the Rhino, "\w" is equvalent to "a-zA-Z0-9_".

    I wouldn't use "." since it will match anything except a newline. It will match whitespace characters.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  14. #14
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    I don't think that [\w-.] will match any character, If it does use
    [\w-\.]

    Otherwise you'll miss out on email addresses like [email protected]

    cudabean

  15. #15
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Now that is a good point (and different from your previous one).

    Code:
    /^[\w-\.]+@([\w-]+\.)+[a-zA-Z]{2,4}$/
    I think that works better.

    For that matter, we can start testing to see if this would be better:

    Code:
    /^\S+@([\w-]+\.)+[a-zA-Z]{2,4}$/
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

Posting Permissions

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



Click Here to Expand Forum to Full Width