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
There are some really nice examples here and also explains the segments quite well: http://www.linuxjournal.com/article/9585
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
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
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
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
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
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).
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])+)\])