Home > Articles, Internet Applications > A simple PHP function that implements email validation.

A simple PHP function that implements email validation.

In this article I’ll show you a simple PHP function that implements email validation.

The function performs two checks :

It checks both if the email has correct syntax and if the domain of the email exists.

Function Implementation :

// Try to check an email address (syntax validation / domain existence).
function valid_email ($email)
{
  // Check regular expression intelligence here: https://github.com/php/php-src/blob/master/ext/filter/logical_filters.c

  // Regular expression for a valid email address.
  $regexp = "/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD";

  // Check if the email address is not valid syntactically.
  if (!preg_match ($regexp, $email)) return false;

  // Extract the user and the domain of the email.
  list ($user, $domain) = explode ("@", $email);

  // Check if the domain of the email does not exist.
  if (!checkdnsrr ($domain, 'MX'))
    return false;

  // In any other case the email is valid.
  return true;
}
  1. David Daniël Wouters
    September 22, 2015 at 00:12

    Very nice script!

    I was able to integrate this very easily in an if and for loop for a contact form of a client website, and the code runs just fine with latest PHP version! Also, the way I’ve integrated it also means that the validation function only runs when someone is ready to submit a completely filled form, and only then it checks if the email is valid, this also has the benefit of very low resource use as my PHP only triggers an “invalid” alert if it’s really invalid and only at the time of form submission, thus as a bonus if the address is invalid clearing the entire form and reloading the page as new.

    So many thanks and regards.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: