Soeren_K_K

Email validator

Jan 8th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. <?php
  2.  
  3. function validEmail($email)
  4. {
  5.  
  6.     $isValid = true;
  7.     $atIndex = strrpos($email, "@");
  8.     if (is_bool($atIndex) && !$atIndex)
  9.     {
  10.         $isValid = false;
  11.     }
  12.     else
  13.     {
  14.    
  15.         $domain = substr($email, $atIndex+1);
  16.         $local = substr($email, 0, $atIndex);
  17.         $localLen = strlen($local);
  18.         $domainLen = strlen($domain);
  19.        
  20.         if ($localLen < 1 || $localLen > 64)
  21.         {
  22.             // local part length exceeded
  23.             $isValid = false;
  24.         }
  25.         else if ($domainLen < 1 || $domainLen > 255)
  26.         {
  27.             // domain part length exceeded
  28.             $isValid = false;
  29.         }
  30.         else if ($local[0] == '.' || $local[$localLen-1] == '.')
  31.         {
  32.             // local part starts or ends with '.'
  33.             $isValid = false;
  34.         }
  35.         else if (preg_match('/\\.\\./', $local))
  36.         {
  37.             // local part has two consecutive dots
  38.             $isValid = false;
  39.         }
  40.         else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
  41.         {
  42.             // character not valid in domain part
  43.             $isValid = false;
  44.         }
  45.         else if (preg_match('/\\.\\./', $domain))
  46.         {
  47.             // domain part has two consecutive dots
  48.             $isValid = false;
  49.         }
  50.         else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
  51.         {
  52.        
  53.             // character not valid in local part unless
  54.             // local part is quoted
  55.             if (!preg_match('/^"(\\\\"|[^"])+"$/',
  56.             str_replace("\\\\","",$local)))
  57.             {
  58.                 $isValid = false;
  59.             }
  60.            
  61.         }
  62.         if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
  63.         {
  64.             // domain not found in DNS
  65.             $isValid = false;
  66.         }
  67.        
  68.     }
  69.    
  70.     return $isValid;
  71.    
  72. }
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment