1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help validation a valid email format using preg_match()

Discussion in 'PHP' started by learnwebsitedesigncom, Jun 29, 2010.

  1. #1
    Hi everyone, I need help verifying that an email has a correct email format (email@somewhere.extension) using the preg_match() function. Can someone provide with a working example and explain to me how to make the at (@) symbol be required input after the first text of the email and also how to make the dot (.) be required after the @ symbol.

    Thank You
    SEMrush
     
    learnwebsitedesigncom, Jun 29, 2010 IP
    SEMrush
  2. Zerix

    Zerix Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Zerix, Jun 29, 2010 IP
  3. learnwebsitedesigncom

    learnwebsitedesigncom Active Member

    Messages:
    264
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #3
    I've read the article and I still can't figure out how to have the @ symbol be required after the first part of the email and also can't figure out how to have the dot be required after the text following the @ symbol.

    for example I've tried:

    
    if(!$email){
     die('Enter an email');
    }
    elseif(preg_match('/[^a-zA-Z0-9]+@([a-z])/', $email)){
     die('Enter a valid email');
    }
    else{
     echo 'some text'; 
    }
    
    PHP:
    
    if(!$email){
     die('Enter an email');
    }
    elseif(preg_match('/[^a-zA-Z0-9]*@([a-z])/', $email)){
     die('Enter a valid email');
    }
    else{
     echo 'some text'; 
    }
    
    PHP:
    and other variations.

    Thank You
     
    learnwebsitedesigncom, Jun 29, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    It would look like the following:

    if(!$email){
     die('Enter an email');
    }
    elseif(!preg_match('/^[a-z0-9]+@[a-z\.]+$/i', $email)){
     die('Enter a valid email');
    }
    else{
     echo 'some text';
    }
    PHP:
    however this is not a complete regex for an email, I suggest you google for php email verification theirs alot better email regex's out their which would work more accurately
     
    Last edited: Jun 29, 2010
    danx10, Jun 29, 2010 IP
  5. learnwebsitedesigncom

    learnwebsitedesigncom Active Member

    Messages:
    264
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #5
    I still am not able to get it to validate the email. The code that I tried is:

    
    if(!$email){
     die('Enter an email');
    }
    elseif(preg_match("/^[a-z0-9]+@[a-z\.]+$/i", $email)){
     die('Enter a valid email');
    }
    else{
     echo 'some text';
    }
    
    PHP:
    Also, I have a few questions about the code:

    
    elseif(preg_match("/^[a-z0-9]+@[a-z\.]+$/i", $email))
    
    PHP:
    Do the characters "+@" mean that what follows is required?
    Does the backwards slash (\) followed by the period (\.) mean that a period is required after the characters that follow the @ symbol?

    Thank You
     
    learnwebsitedesigncom, Jun 29, 2010 IP
  6. prasanthmj

    prasanthmj Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    45
    #6
    Give details. when you say 'I still am not able to get it to validate', explain what exactly is going wrong? Sample email address that does not validate, the error that you are getting etc.

    Better :
    
    if(empty($email)){
     die('Enter an email');
    }
    elseif(preg_match("/^[a-z0-9]+@[a-z\.]+$/i", $email)){
     die('Enter a valid email');
    }
    
    PHP:
    No; [a-z0-9]+ means one or more alpha-numeric chars.
    Read the Regular expression reference for details
    also see
    More details on regular expression for email
     
    prasanthmj, Jun 29, 2010 IP
  7. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    note for ANYONE doing regex validation: please make sure that the characters '+' and '.' are both allowed, seriously. if you're not going to completely conform to the rfc spec, at least allow those common characters
     
    krsix, Jun 29, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    Yes, but I posted to the OP's requirements.
     
    danx10, Jun 29, 2010 IP
  9. learnwebsitedesigncom

    learnwebsitedesigncom Active Member

    Messages:
    264
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #9
    I am now able to validate an email in the format that I want: ion. This is the code that I have.

    
    if(!$email){
     die('Enter an email');
    }
    if (!preg_match("/^[a-zA-Z0-9]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]/", $email)){
     die('Enter a valid email.');
    }
    else{
     echo 'some text';
     }
    
    PHP:
    If you can give me your opinions on the code, a different version of code that works, etc. I'd appreciate it.

    Also, I forgot to mention that I'm filtering (for lack of a better word I think) through the function htmlspecialchars() if that matters as far as this validation code goes.

    
    $email = htmlspecialchars($_POST['email']);
    
    PHP:
    Also, with regard to what both of you typed earlier, are you referring to this: http://www.w3.org/Protocols/rfc2616/rfc2616.html (btw, I don't know what it is at the moment, I just Googled it).

     
    learnwebsitedesigncom, Jun 29, 2010 IP
  10. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #10
    nah, it's just something like #(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
     
    krsix, Jun 29, 2010 IP