SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Addict sstaubin01's Avatar
    Join Date
    Nov 2000
    Location
    Ont, Canada
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to build a Regular Expression used with PHP to edit a form field containing an Email Address. I need the field to include a @ and a . otherwise it is an invaild value. The Regular Expressions I have experimented with use an 'or' operator but this must be an 'and' operator. The text must include a @ and . The Regular Expression [@.] is true if the text includes @ or .

    Any suggestions?

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the pattern I have, spot out any error if you see!

    Code:
    ^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    SitePoint Addict sstaubin01's Avatar
    Join Date
    Nov 2000
    Location
    Ont, Canada
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks 123. I don't know what all that means. My examples were much simplier. I'll research the logic, try it out and let you know if I find any problems. Thanks again.

  4. #4
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the one I use - it aint perfect but it allows
    me.at@this.com.au etc which some routines do not

    <?
    function vmail($email) {global $res;
    $re="^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
    if(eregi($re,$email)){$res="pass";} else { $res="fail";}
    }?>

    It will however allow bad ones like this@that.com.me.you.doh

  5. #5
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, this is good!
    I though .+ would accept any character, but when I tested your function with +=..., it failed (which is excellent!)

    So do we have an optimal solution for this problem?
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  6. #6
    SitePoint Addict sstaubin01's Avatar
    Join Date
    Nov 2000
    Location
    Ont, Canada
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you guys tell me what's wrong with this. It accepts anything.

    // Regular Expression for validating emailaddr.
    function vmail($emailaddr) {global $emailspecialcharerror;
    $re="^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
    if(eregi($re,$emailaddr)){$emailspecialcharerror = 'yes'; $error = 'yes';}}

    123 says it works. I must be doing something wrong.

  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)
    This one seems to work well for me:

    function bad_email($emailtotest) {
    return eregi("(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)", $emailtotest, $arr_vars) OR !eregi ("^.+\@(\[?)[-a-zA-Z0-9\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$", $emailtotest, $arr_vars);
    }

    if (bad_email("john@johnson.com")) {
    print "Email Bad";
    }
    else {
    print "Email good";
    }

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by sstaubin01
    Can you guys tell me what's wrong with this. It accepts anything.

    // Regular Expression for validating emailaddr.
    function vmail($emailaddr) {global $emailspecialcharerror;
    $re="^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
    if(eregi($re,$emailaddr)){$emailspecialcharerror = 'yes'; $error = 'yes';}}

    123 says it works. I must be doing something wrong.
    For the else part, you should set it to be something else.
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  9. #9
    SitePoint Addict sstaubin01's Avatar
    Join Date
    Nov 2000
    Location
    Ont, Canada
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried Freddy's pattern and it works as coded in the post although it has the same problem as Firepages stated. It accepts aa@aa.aa.bb.ccc. A great deal of respect goes out the the people who figure this stuff out. Thanks all for the help.

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but that is valid haven't you seen email address like

    someone@univ.tenn.edu

    or someone@streets.ca.gov
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  11. #11
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Freddy, do you mind to explain the pattern you had?

    Code:
    (@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)
    Could you list a case for each "or" you had?

    Thanks, since I just want to learn more about reg. exp.
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  12. #12
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if(eregi($re,$emailaddr)){$emailspecialcharerror = 'yes'; $error = 'yes';}}

    should be
    if(eregi($re,$emailaddr)){//this is good//}else{//this is bad//}

    and yes its good to allow '.com.au' for instance but it still does not cover silly stuff like .com.au.haha.yuk etc

    and I have never figured out a bit of REGEX in my life ! (well almost never) - this is an old javascript one I 'borrowed' from I know not where, but it gets used every day so far with no probs, apart from the odd joker trying to see if he/she can pass dodgy code.

  13. #13
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Son,

    eregi("(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)

    This says that if there are
    (@.*@) = two @ in email address
    (\.\.) = two .. right next to each other in the address
    (@\.) = two @. right next to each other in the address
    (\.@) = two .@ right next to each other in the address
    (^\.) = one . right at the beginning of the address
    //This one was missing from the above(\.$) = one . right at the end of the address
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  14. #14
    SitePoint Wizard jumpthru's Avatar
    Join Date
    Apr 2000
    Location
    Los Angeles, California
    Posts
    1,008
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just take a brief over look of this thread, from top to bottom.

    Now you know why most programmers go crazy (and probably why so many newbies get scared off).

  15. #15
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freddydoesphp
    Son,

    eregi("(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)

    This says that if there are
    (@.*@) = two @ in email address
    (\.\.) = two .. right next to each other in the address
    (@\.) = two @. right next to each other in the address
    (\.@) = two .@ right next to each other in the address
    (^\.) = one . right at the beginning of the address
    //This one was missing from the above(\.$) = one . right at the end of the address
    Nice! Do you think we could catch all the cases?
    And one interesting thing: for the new domains (.info, .aero,...) the ending match will fail (only allows 2,3 chars)
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  16. #16
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Should be easy enough to fix
    function bad_email($emailtotest) {
    return eregi("(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)", $emailtotest, $arr_vars) OR !eregi ("^.+\@(\[?)[-a-zA-Z0-9\.]+\.([a-zA-Z]{2-4}|[0-9]{2-4})(\]?)$", $emailtotest, $arr_vars);
    }

    What else would you like to see it catch?

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  17. #17
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well,
    Code:
    ([a-zA-Z]{2-4}|[0-9]{2-4})(\]?)$
    Should it be {2,4} instead of "-"?

    Yeah, I think it's more than enough when it could catch those invalid email! Or else your visitor will yell and let you know why their email got rejected!!
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  18. #18
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are correct Sir!

    That is what I get for posting before I test
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  19. #19
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freddydoesphp
    You are correct Sir!

    That is what I get for posting before I test
    LOL!
    About the fix for new domains (4chars), it is easy but I believe MANY error checking patterns are in use will fail. Let see the effect in Jan 2001 (is that the time we are allowed to use those domain??)
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  20. #20
    SitePoint Addict
    Join Date
    Nov 2000
    Location
    London, UK
    Posts
    223
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok,
    I know this isn't regex, but anyways... Would this correctly check a file for a certain email address and return true if it was there and false if not (or vice versa...):
    Code:
    function email_already_exists($emailtotest) {
    return eregi($emailtotest,implode(file($listfile),""),$arr_vars);
    }
    Thanks!

    ... what's the world coming to?

  21. #21
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The dots in the email address have a special meaning when used as a regular expression. Better to use a simpler (and faster) string search function:

    Code:
    function email_already_exists($emailtotest) {
      return (substr_count(implode(file($listfile),""),$emailtotest) > 0);
    }
    Last edited by Kevin Yank; Feb 21, 2001 at 09:55.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  22. #22
    SitePoint Addict
    Join Date
    Nov 2000
    Location
    London, UK
    Posts
    223
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see ...
    And so I could use that as in
    Code:
    if (!email_already_exists($emailaddress)){
    do blahblah; }
    and it'll do it only if the email address is not already in the file? or would I need something like
    Code:
    if (!email_already_exists($emailaddress)>0){
    do blahblah;}
    Thanks a lot for your help
    BTW: the script this is being used for is here:
    http://www.dannycook.com/index.php3?...le=mailinglist
    this actually includes it but you can see the basic functionality. source code is at
    http://www.dannycook.com/source/mailinglist.php3.txt
    (edit)The rest of the php source code for the site is in that directory as well:
    http://www.dannycook.com/source/(end edit)

    Thanks!
    Last edited by FatPiper; Feb 21, 2001 at 15:32.

    ... what's the world coming to?

  23. #23
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Your first code fragment is correct. The function converts the numeric result to true/false already.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  24. #24
    SitePoint Addict
    Join Date
    Nov 2000
    Location
    London, UK
    Posts
    223
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, great ... Nothing like SP!

    ... what's the world coming to?


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
  •