PHP Script to validate an Email Address

I do a lot of programming that requires validation of user data – including a users email address.

When I am collecting this data via a Web Form, I like to use the JQuery Validate plugin to give immediate feedback to my user when they have not entered an e-mail address properly.

In addition, I like to sanitize a users input once it gets to the server, and validate it again using the server code before storing the data.

Below is a small and simple PHP script to validate an Email Address before storing it into your data store.

 * Validates if a string is a valid email address
 * @param String $email The string to vlidate as an email address
 * @return bool|String The email if valid, otherwise a boolean false
 function isEmail($email) {
   //email is not case sensitive so force it lower case
   $email = strtolower($email);
   //check if email has a valid pattern match
   if (preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/", $email)) {
     return $email;
   return false;

As you can see the php script to validate an email address is very small and simple.  It’s designed to be fast and quick.  I hope it helps!

Posted in , and tagged , , .