Search code examples
regexvalidationemail

Email regular expression


Duplicate: What is the best regular expression for validating email addresses?

I know this is a common question, but I still can't seem to find a great regular expression to use when validating email addresses.

I don't really have time to go read the spec and write my own. What have ya'll used before, and has it worked well? I don't really care about 100% matching the spec, but the closer the better.


Solution

  • Here's a function that I use. It does a little more than just run the email address through a regex, but so far it is the most complete solution that I found:

    function validEmail($email, $skipDNS = false)
    {
       $isValid = true;
       $atIndex = strrpos($email, "@");
       if (is_bool($atIndex) && !$atIndex)
       {
          $isValid = false;
       }
       else
       {
          $domain = substr($email, $atIndex+1);
          $local = substr($email, 0, $atIndex);
          $localLen = strlen($local);
          $domainLen = strlen($domain);
          if ($localLen < 1 || $localLen > 64)
          {
             // local part length exceeded
             $isValid = false;
          }
          else if ($domainLen < 1 || $domainLen > 255)
          {
             // domain part length exceeded
             $isValid = false;
          }
          else if ($local[0] == '.' || $local[$localLen-1] == '.')
          {
             // local part starts or ends with '.'
             $isValid = false;
          }
          else if (preg_match('/\\.\\./', $local))
          {
             // local part has two consecutive dots
             $isValid = false;
          }
          else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
          {
             // character not valid in domain part
             $isValid = false;
          }
          else if (preg_match('/\\.\\./', $domain))
          {
             // domain part has two consecutive dots
             $isValid = false;
          }
          else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
          {
             // character not valid in local part unless 
             // local part is quoted
             if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local)))
             {
                $isValid = false;
             }
          }
          
          if(!$skipDNS)
          {
              if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
              {
                 // domain not found in DNS
                 $isValid = false;
              }
          }
       }
       return $isValid;
    }
    

    The function has an optional $skipDNS argument that can be set to TRUE if you don't want to validate the MX records for the hos. Otherwise the function will attempt to validate that the e-mail address provided actually maps to a real email server.

    It's useful to note that most RegEx email validation techniques will validate most e-mail addresses but they will most likely allow some carefully crafted invalid addresses or worst.. fail on some more obscure, but valid e-mail addresses. For more information you may want to check out the Internet Message Formats RFC which describes the allowed format for e-mail addresses.