Email validation with PHP

6 Juli 2011 - PHP

After I had posted you a validation script with JavaScript I will now give you the script for validating an email address with PHP. The JavaScript version was good for a validation directly while typing the address in the field. With this PHP script the address will be validated after sending the form to the server.
The script checks the address, if the length of the string is within the allowed borders, if it includes an @ sign, if the address part do not start or stops with a dot, if it do not have two consecutive dots, if there are no unallowed signs in the string and if the domain part is a valid domain.

function validateEmailaddress($email) {
  $isValid = true;
  $indexOfAtSign = strrpos($email, "@");
  if (is_bool($indexOfAtSign) && !$indexOfAtSign) {
    //if no @ included, address is not valid
    $isValid = false;
  } else {
    // get domain part
    $domain = substr($email, $indexOfAtSign+1);
    // get specific address part
    $specific = substr($email, 0, $indexOfAtSign);
    // get length of the specific address part
    $specificLen = strlen($specific);
    // get length of domain part
    $domainLen = strlen($domain);
    if ($specificLen < 1 || $specificLen > 64) {
      // specific length part cannot be less then one sign
      // and not more than 64 signs
      $isValid = false;
    } else if ($domainLen < 1 || $domainLen > 255) {
      // domain part cannot be less then one sign
      // and not more than 255 signs
      $isValid = false;
    } else if ($specific[0] == '.' || $specific[$specificLen-1] == '.') {
      // specific address part cannot start or end with a dot
      $isValid = false;
    } else if (preg_match('/\\.\\./', $specific)) {
      // specific address part cannot have consecutive dots
      $isValid = false;
    } else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
      // domain part has not valid signs included
      $isValid = false;
    } else if (preg_match('/\\.\\./', $domain)) {
      // domain part part cannot have consecutive dots
      $isValid = false;
    } else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
      str_replace("\\\\","",$specific))) {
      // character not valid in local part unless specific
      // address part is quoted
      if (!preg_match('/^"(\\\\"|[^"])+"$/',
        str_replace("\\\\","",$specific))) {
        $isValid = false;
    if ($isValid && !(checkdnsrr($domain,"MX") ||
      checkdnsrr($domain,"A"))) {
      // domain not found in DNS
      $isValid = false;
  return $isValid;

For validating the email address string you have to call this function with the address as parameter. It returns ‚true‘ if the address is a valid email address and ‚false‘ if not.
if(validateEmailaddress('')) {
  //do something
} else {
  // send user message, that address is not valid


Print Friendly, PDF & Email

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.