KonsultanITBandung

Email Validator

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