Friday, September 15, 2006

PHP Email Validation using Regular Expressions

This is a very geeky post, but I spent a good three hours making this code work perfectly, and I'm kind of proud of it, so there.

The idea is this: I needed a function that would validate the email address entered by a user on a new Tech Request System that I'm writing for Minnehaha, but I also wanted the user to be able to enter comma separated email addresses in the form and have those parsed correctly. Google didn't seem to have a ready-made function available for me to use, so I knew I had to take on the challenge and write my own. It was fun, too, so I can't really complain. The code may not be the most efficient it could be, but it works, and for a small scale application like this that's really the only requirement.


The following PHP function, validateEmail($email), checks the input ($email) to see if it contains valid email addresses. This is done first by parsing the input string into an array, each location in which will contain an individual email address to be parsed (this is assuming the original input was a comma separated list of emails; if only one email address was entered, this array will only have value associated with it). I then create a variable to return (cleverly named $return) and initialise it to be the returned value from calling validateSingleEmail on the first item in the aforementioned array. If there are more items in the array, the for loop goes through and checks them by calling validateSingleEmail on each. If the item is a valid email, that text is returned and appended (with a comma and space) to the $return variable. If the item is a blank email (pure whitespace, that is), validateSingleEmail returns an empty string and nothing is appended to $return (this is useful to catch any trailing commas and spaces in the original input so that they don't cause an error). Otherwise, if the item is an invalid email address (not matching the format abc@def.ghi), a validateSingleEmail returns false, thus causing validateEmail to also return false, which, in my application, causes an error message to appear on the user's screen when they press submit.

If all goes well, though, the function returns $return, and also sets the POST variable $_POST["Email"] to be this return value (that way everything is entered into the database correctly when the rest of the data is sent).

That's it. Very exciting for the non-computer people, I know. Actually, I'm sure it's not all that terribly exciting for the computer types, either, but maybe it will be useful to someone else someday.


function validateEmail($email) {
  $emails = preg_split("/,/", $email);
  if($return = validateSingleEmail($emails[0])) {
    $count = count($emails);
    for($i = 1; $i < $count; $i++) {
      $addition = validateSingleEmail($emails[$i]);
      if($addition !== false) {
        $return .= ($addition == '' ? '' : (', ' . $addition));
      } else {
        return false;
      }
    }
    $_POST["Email"] = $return;
    return true;
  } else {
    return false;
  }
}

function validateSingleEmail($email) {
  if(!preg_match('/[^ ]+\@[^@ ]+\.[^@ ]+/', $email, $matches)) {
    if(preg_match('/[\S]+/', $email, $matches)) {
      return false;
    } else {
      return '';
    }
  } else {
    return $matches[0];
  }
}

No comments: