Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Validating an Email Address

    Hi there,

    Can someone explain this piece of code to me? I get the general idea here but can you break it down for me as I don't just want to copy and paste it without fully understanding it!

    Thank you so much in advance!

    Here it is:


    /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,5}$/i

  • #2
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Also,

    Can you tell me what this means?

    /^\s*$/

  • #3
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    OK, its hard to work out which is the best way to explain in this medium but I'll have a go.

    I'll work from left to right.

    ok, then;

    the / is the first boundary of the regex.
    the ^ means the beginning of your regex manipulation
    the first [] section shows what chars are allowed in the first part of the email address.
    the + means that each of those chars can occur more than once
    the @ symbol represents the @ in the address
    the second [] section shows the chars allowed after the @ symbol and before the .tld
    again, the + means more than one occurence is accepted
    \ means escape the .
    [A-Z] allows for all alphabetical chars for the domain suffix (tld)
    {2,5} means minimum of 2 max of 5 chars in the tld
    $ means the end of your manipulation
    / denotes the end of the regex and
    i means case insensitive

    It should be noted that using the ^ and $ means you are checking the complete snippet. if you just use // and no ^ or $, then you are chekcing for a match anywhere, whereas the ^ and $ means that the match has to match the whole thing from start to end. I'm sure there is an easier way to explain that but I hope that helps

    the '5' needs to be changed to about 7 since tld's can have more than 5 letters. not sure the max number though. Please note I am not evaluating whther that is in fact a good regex for your purpose. O'Reilly does a good book on the subject (and others) at your fav online bookshop.

    ps, are you from Emerald Isle?
    bazz
    Last edited by bazz; 04-16-2009 at 12:52 AM.
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • The Following 2 Users Say Thank You to bazz For This Useful Post:

    Fun Leprechaun (04-17-2009), TinyScript (04-16-2009)

  • #4
    The Apostate Apostropartheid's Avatar
    Join Date
    Oct 2007
    Posts
    3,215
    Thanks
    16
    Thanked 265 Times in 263 Posts
    Not really a JavaScript man, but wouldn't that fail when presented with emails whose issuing website is registered under a third-level domain (e.g. *.co.uk)?

  • #5
    Regular Coder
    Join Date
    Mar 2009
    Location
    Portland Oregon
    Posts
    690
    Thanks
    44
    Thanked 63 Times in 62 Posts
    Quote Originally Posted by bazz View Post
    OK, its hard to work out which is the best way to explain in this medium but I'll have a go.

    I'll work from left to right.

    ok, then;

    the / is the first boundary of the regex.
    the ^ means the beginning of your regex manipulation
    the first [] section shows what chars are allowed in the first part of the email address.
    the + means that each of those chars can occur more than once
    the @ symbol represents the @ in the address
    the second [] section shows the chars allowed after the @ symbol and before the .tld
    again, the + means more than one occurence is accepted
    \ means escape the .
    [A-Z] allows for all alphabetical chars for the domain suffix (tld)
    {2,5} means minimum of 2 max of 5 chars in the tld
    $ means the end of your manipulation
    / denotes the end of the regex and
    i means case insensitive

    It should be noted that using the ^ and $ means you are checking the complete snippet. if you just use // and no ^ or $, then you are chekcing for a match anywhere, whereas the ^ and $ means that the match has to match the whole thing from start to end. I'm sure there is an easier way to explain that but I hope that helps

    the '5' needs to be changed to about 7 since tld's can have more than 5 letters. not sure the max number though. Please note I am not evaluating whther that is in fact a good regex for your purpose. O'Reilly does a good book on the subject (and others) at your fav online bookshop.

    ps, are you from Emerald Isle?
    bazz
    Can I save that on a web page for easy reference? I just wanted to ask first in case you wanted to make tutorial page. That's a sweet explanation

  • #6
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Quote Originally Posted by CyanLight View Post
    Not really a JavaScript man, but wouldn't that fail when presented with emails whose issuing website is registered under a third-level domain (e.g. *.co.uk)?
    yeh I think it would.

    I doubt that it is a very good regex. I would need to hoke out the O'reilly book but I seem to recall the example they gave is much longer and more difficult to read.


    @TinyScript: I don't see a problem with you copying it. No need to attribute it to me either.

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    I find the best email regex is:-

    if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(eMailAddress.value))) {


    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.

  • #8
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    yeh, Philip, thats more like the O'Reilly one I referred to though I can't place the book.

    re. your sig.

    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.
    I can relate to that

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


  •  

    Posting Permissions

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