SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard bronze trophy bigalreturns's Avatar
    Join Date
    Mar 2006
    Location
    The Wirral, England
    Posts
    1,293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regex to validate email

    I'm trying to write a regex that will optimally allow any valid email, and disallow any invalid one.
    The rules I've assumed are:
    1) Any combination of letters/numbers/dots/underscores before the @ symbol
    2)a domain of at least 2 letters, with a tld of either exactly 3 letters (ie .com), or 2.2 ie .co.uk

    Are these rules accurate/adequate?
    Is the regex below a decent representation of these rules?

    Bear in mind I'm still learning regex, so please be constructive with any criticism!



    ^[a-zA-Z0-9\._]+@[a-zA-Z0-9\.]{2,}\.([a-zA-Z]{3}|([a-zA-Z0-9]{2}\.[a-zA-Z0-9]{2}))$
    "The proper function of man is to live - not to exist."
    Get a Free TomTom


  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This one's pretty complete (from the Firefox Regular Expression Tester Extension):
    Code:
    ^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$
    Yours doesn't allow for all those funny TLDs. Also, it assumes that the address will end with something like .com or .net or .co.uk or .co.jp, but not simply .fr or .ws. It'll also fail for .com.ar, .net.pe and the likes.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to consider checking to see if the domain of the email address you're validating is actually registered. A function like getmxrr() should do the trick (after separating the username from the domain, of course).
    MattsBlog.Ca
    LiveMP -- Coming soon!

  4. #4
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's usually best to be as permissive as you can with this sort of thing (though possibly not depending on what you're doing).
    I usually just check to make sure it look somewhat like a valid email; but I certainly don't check for individual TLDs and the like. You also have to watch out for funny charsets, your current regex won't allow for any non-English foreign characters.

    If you try too hard to create the perfect email validating regular expression, you might end up with something like this

  5. #5
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trib4lmaniac View Post
    If you try too hard to create the perfect email validating regular expression, you might end up with something like this

    Thats great, I'm now wondering who sat and worked that out, thats on the wall for sure. Thanks
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Location
    Canada
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trib4lmaniac View Post
    It's usually best to be as permissive as you can with this sort of thing (though possibly not depending on what you're doing).
    I usually just check to make sure it look somewhat like a valid email; but I certainly don't check for individual TLDs and the like. You also have to watch out for funny charsets, your current regex won't allow for any non-English foreign characters.

    If you try too hard to create the perfect email validating regular expression, you might end up with something like this
    Funny, but that code just repeats itself, so it's not that funny.
    MattsBlog.Ca
    LiveMP -- Coming soon!

  7. #7
    SitePoint Wizard bronze trophy bigalreturns's Avatar
    Join Date
    Mar 2006
    Location
    The Wirral, England
    Posts
    1,293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    This one's pretty complete (from the Firefox Regular Expression Tester Extension):
    Code:
    ^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$
    Yours doesn't allow for all those funny TLDs. Also, it assumes that the address will end with something like .com or .net or .co.uk or .co.jp, but not simply .fr or .ws. It'll also fail for .com.ar, .net.pe and the likes.
    Thanks for the info.

    Quote Originally Posted by Speedboxer View Post
    You might want to consider checking to see if the domain of the email address you're validating is actually registered. A function like getmxrr() should do the trick (after separating the username from the domain, of course).
    From the page you link to...

    Note: This function should not be used for the purposes of address verification. Only the mailexchangers found in DNS are returned, however, according to RFC 2821 when no mail exchangers are listed, hostname itself should be used as the only mail exchanger with a priority of 0.
    "The proper function of man is to live - not to exist."
    Get a Free TomTom


  8. #8
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this function does the job for me, it is probably more an accept all type, but at least it stops muppets from entering email@myemail. etc.
    PHP Code:
    function valid_email($email)
    {

        
    $pattern '/^\w[-.\w]*@([-a-z0-9]+\.)+[a-z]{2,4}$/i';
        
        if (!
    preg_match($pattern,$email)) return false; else return true;
        

    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  9. #9
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Brooklyn, NY
    Posts
    359
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the only good approach I've seen:

    http://iamcal.com/publish/articles/php/parsing_email
    Chris Shiflett
    http://shiflett.org/


Bookmarks

Posting Permissions

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