Check Email with Regular Expressions (Regex)
For web developers, validating email addresses is just part of what it means to accept user input. One of the challenges I find is that email validation does not often stay up to date with the changes in email. For instance, Gmail allows for cool features like adding a +anything to the end of your email address. Many regex email validation strings do not allow for this useful function. Well, in designing the signup for patterntap.com I ran into this, when of course initially my regex did not allow for the gmail + or even a four letter domain name (e.g. .info) So.. here is my updated regex for PHP. Hope you find it userful, please comment if you find any issues with it.
// lets just make the email lowercase for the ALL CAPS people $email = strtolower($_REQUEST["emailreal"]); if (eregi("^[\.\+_a-z0-9-]+@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$", $email) ) { // now do something cool! }















2 Comments, Comment or Ping
Dennis Frank
I ran into both validation issues. I tried an .info address and and a gmail+ address on patterntab.com.
Everythings works fine now. Thanks!
Mar 21st, 2008
Reply to “Check Email with Regular Expressions (Regex)”