SitePoint Sponsor

User Tag List

Page 1 of 4 1234 LastLast
Results 1 to 25 of 77
  1. #1
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validate well formed email

    At the risk of upsetting sensitive penguins, I'm going to once again raise the issue of validating well formed email addresses. I've tried several regular expressions (some that I have found from going over old posts here). None of them seem to work for me.

    I want to also test that the email ends in either a two letter country suffix or a three letter top level domain suffix.

    Here is what I have tried, which doesn't seem to work for me:

    eregi("^[a-z0-9\._-]+@[a-z0-9\._-]+\.+[a-z]{2,3}$", $email)

    I have tried another couple of expression as well but I'm too confused right this minute to work out what they are

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The - needs to be at the beginning.

  3. #3
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,939
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of doing a-z0-9 why not do [^]?


    some people do have emails like this

    peter.smith@domain.com

    Anyways...

    [^] = any number of characters

    [^@] = any number of characters not an @
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  4. #4
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    He's not using ^ because he only wants certain characters. Yes, some people do have emails like peter.smith@domain.com, and his script will work correctly with it.

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have been working on this same thing for awhile now, I mean I have used some of the expressions out there and they work for most cases, but I have yet to come across one that will fit all cases. So here is what I propose, we all put some email address down here and then we can try to come up with something that works for all scenarios. I think if we all contribute we can cover most cases.

    john@domain.com
    john.smith@domain.com
    john.smith@domain.netcom.com
    john.smith@domain.co.uk
    john.smith@domain.co.au



    What are some others that need to work?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Those will all work with freakysid's regex...

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anarchos thats my point, obviously freakysid is having trouble with some email addresses. So I thought if we all put down some variations we can come up with, then we can possibly write a better expression to cover all the possibilities, right?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I see your point, but the reason it wasn't working is because he didn't put the dash at the beginning. Otherwise, it should work for all valid email addresses.

  9. #9
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the thing that is not working at the moment is my brain, due to lack of sleep.

    Here is another example not in the list (essentially it is all the domains of countries which do not use a "com" or "co" etc with their two letter country code:

    eg:

    john.smith@domain.to
    smith@domain.to

  10. #10
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you everyone!

    Here is my validation function
    PHP Code:
    function validateWellFormedEmail($email) {
       return 
    eregi("^[-a-z0-9\\._]+@[-a-z0-9\\._]+\\.+[a-z]{2,3}$"$email);

    The following test cases all pass. Woo-hoo!

    john@domain.com
    john.smith@domain.com
    john.smith@domain.netcom.com
    john.smith@domain.co.uk
    john.smith@domain.com.au
    john.smith@domain.to
    smith@domain.to
    smith@subdomain.domain.to

    I would just like to have my belief confirmed that an email address cannot be case sensitive. Is this right? for example,

    Joe.Jones@foo.bar

    is equivelent to

    joe.jones@foo.bar
    Last edited by freakysid; Jul 16, 2001 at 18:11.

  11. #11
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i notice there's no backslashes in front of the periods but i guess that's just stupid VB.

    i wanted to ask about this part though.

    \.+[a-z]{2,3}

    why the + after the . if you only want ONE dot matched? the way it is now, it would match john@domain....com

    see?

  12. #12
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freakysid
    I would just like to have my belief confirmed that an email address cannot be case sensitive. Is this right? for example,

    Joe.Jones@foo.bar

    is equivelent to

    joe.jones@foo.bar
    AFAIK, yes. i GUESS it's possible that certain mail servers could be configured to be case sensitive, but i've never heard of it. and i think it would be silly for one to be case sensitive. (i.e. non comp savvy people could type it wrong and it wouldn't work, etc.)

  13. #13
    SitePoint Zealot NZ Joe's Avatar
    Join Date
    Apr 2001
    Location
    Kapiti, New Zealand
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know about much regular expressions so this may be a stupid question, but does this work for underscores?
    john_smith@domain.tld
    Gravity always wins

  14. #14
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All these pass:

    john_smith@domain.tld
    _john_smith@domain.tld
    john_smith@_domain.tld

    Should the last case have passed? Is _domain.tld a valid domain name?

    (looking at my post, vB doesn't seem to think so)

  15. #15
    SitePoint Zealot NZ Joe's Avatar
    Join Date
    Apr 2001
    Location
    Kapiti, New Zealand
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    _domain.tld is not valid, however I'm not sure about _subdomain.domain.tld
    Gravity always wins

  16. #16
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by DR_LaRRY_PEpPeR
    i notice there's no backslashes in front of the periods but i guess that's just stupid VB.

    i wanted to ask about this part though.

    \.+[a-z]{2,3}

    why the + after the . if you only want ONE dot matched? the way it is now, it would match john@domain....com

    see?
    That is correct john@domain....com passed when it should have failed.

    So now we have two test cases which produce the wrong result:
    john@domain....com
    john_smith@_domain.tld

    and we are not sure about whether this should pass or fail:
    smith@_subdomain.domain.tld

    Please help me out here. I am a pathetic copy-and-paste regex hacker. I have done quite a bit of searching on this and I am suprised that there does not seem to be a set of robust validation regex (that is, if it requires more than one) to test for a well formed email. Well, I have not found it yet

  17. #17
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to complicate it further, my email as uni is

    christopher.padfield@new.ox.ac.uk

    so it has 4 sections after the @ sign

  18. #18
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The 4 sections doesn't matter, it'll still parse fine using the initial regex, and I don't see why anyone has a problem with that one. Regex validation cannot be used for anything more than user error, and the easiest way to do this is to either send a confirmation email or use a confirmation email input.

  19. #19
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anarchos, I agree fully that regex cannot fill in my tax return or mow the lawn. In fact, I do send an email to new members and if the email bounces, Sendmail pipes the bounced email to a script which deals with matters from there. However, the audience of the site I am developing for cannot be assumed to know how to tie their shoe-laces. So, as much as possible I want to validate the user input while I have the opportunity of having the user re-enter bad data.

    If you look at my last post above, you will see that vBulletin does not recognise the three emails I list as being valid email addresses. So vBulletin knows how to do this

  20. #20
    SitePoint Zealot NZ Joe's Avatar
    Join Date
    Apr 2001
    Location
    Kapiti, New Zealand
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found this in my travels today. It may be of use.
    http://phpclasses.upperdesign.com/br...tml/package/13
    Gravity always wins

  21. #21
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks NZ Joe. I busted open that class and found the regex, which is below (regexB)

    So now I have tested two regex

    regexA:
    "^[-a-z0-9\._]+@[-a-z0-9\._]+\.+[a-z]{2,3}$"

    regexB:
    "^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~ ])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~ ]+\\.)+[a-zA-Z]{2,4}\$"

    Here is the test code (I just passed the value of $e in the URL)
    PHP Code:
    <?= eregi("^[-a-z0-9\\._]+@[-a-z0-9\\._]+\\.+[a-z]{2,3}$"$email) ? 'TRUE' 'FALSE'?>
    <br>
    <?= eregi("^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~ ])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~ ]+\\.)+[a-zA-Z]{2,4}\$"$email) ? 'TRUE' 'FALSE'?>
    Here are the test cases I put them both through.

    Test Cases (expected result: TRUE)
    1. john@domain.com
    2. john.smith@domain.com
    3. john.smith@domain.netcom.com
    4. john.smith@domain.co.uk
    5. john.smith@domain.com.au
    6. john.smith@domain.to
    7. smith@subdomain.domain.to
    8. smith@domain.to
    9. john@some-domain.com
    10. john_smith@some_domain.com
    11.john@domain_42.com
    12.christopher.padfield@new.ox.ac.uk

    Test Cases (expected result: FALSE)

    13.john@domain....com
    14. john_smith@_domain.tld

    Test Case (expected result unknown)
    15. smith@_subdomain.domain.tld

    Results regexA
    Cases 1-12 all passed.
    Cases 13 & 14 failed.
    Case 15 produced a result of TRUE

    Results regexB
    Cases 1 - 13 passed.
    Case 14 failed.
    Case 15 produced a result of TRUE

    So it seems that there is still an opportunity for a wise head to still make improvements to regexB.
    Don't forget that the validation does not have to be achieved with just the one regex.
    I can use multiple regex.
    Last edited by freakysid; Jul 17, 2001 at 11:12.

  22. #22
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a regexp book which lists a 'failproof' regexp for e-mail addresses which is, oh, 600 characters long. If I have the time I will scan it in and let you type it.

  23. #23
    SitePoint Zealot techie2003's Avatar
    Join Date
    May 2001
    Location
    Boston, Massachusetts
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    freakysid: try this:

    jng03@bls.boston.k12.ma.us

    that's 5 i believe
    most school addresses are like that in the US if they're using the ones provided in the city.
    My computer: Dell Latitude CPx | P3 650/500 w/ 256 MB RAM | 15" 1024x768 | IE 5.5 DSL
    gO rEd SoX! | "The greatest thing you'll ever learn is just to love and be loved in return" - Christian, Moulin Rouge
    l e a r n t o q u e s t i o n | c l i o 3 6 0

  24. #24
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by MattR
    I have a regexp book which lists a 'failproof' regexp for e-mail addresses which is, oh, 600 characters long. If I have the time I will scan it in and let you type it.
    Perhaps they have a web site where code in the book is available for dl?


    BTW, techie2003 jng03@bls.boston.k12.ma.us passes

  25. #25
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've got a book which recommends this:

    http://www.zend.com/codex.php?id=88&single=1

    I have not tried it

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature


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
  •