PHP function to check valid email address and TLD

Sunday, August 28, 2011

Validating email is mandatory process to maintain quality email database of website visitors. But cetertain situation we need to accept simple validating for E-mail Address using @ and . or validating using Regular expression may accept unknown domain TLDs, Here is my solution to avoid E-mail address certain range of TLDs.


<?php

// @ Desc: Check Valid Email and TLD;
// @ email as string
// @ Dependancy: Nothing;
function is_valid_email($email) {
   if (substr_count($email, '@') != 1)
      return false;
   if ($email{0} == '@')
      return false;
   if (substr_count($email, '.') < 1)
      return false;
   if (strpos($email, '..') !== false)
      return false;
   $length = strlen($email);
   for ($i = 0; $i < $length; $i++) {
      $c = $email{$i};
      if ($c >= 'A' && $c <= 'Z')
         continue;
      if ($c >= 'a' && $c <= 'z')
         continue;
      if ($c >= '0' && $c <= '9')
         continue;
      if ($c == '@' || $c == '.' || $c == '_' || $c == '-')
         continue;
      return false;
   }
   $TLD = array (
         'COM',   'NET',
         'ORG',   'MIL',
         'EDU',   'GOV',
         'BIZ',   'NAME',
         'MOBI',  'INFO',
         'AERO',  'JOBS',
         'MUSEUM'
      );
   $tld = strtoupper(substr($email, strrpos($email, '.') + 1));
   if (strlen($tld) != 2 && !in_array($tld, $TLD))
      return false;
   return true;
}

?>


Download This Script
    Download Script

No comments:

Post a Comment