Jump to content

regex an email


otuatail

Recommended Posts

Hi I have been given a link to a website here for validating an email. It is very extensive and comes with a list of valid and invalid email address to test. The problem is, one of the emails that fails passes.

 

gatsby@f.sc.

 

this should fail as it ends with a (.)

 

Can anyone find the offending part of the regex.

 

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

// Some Useful VB functions
function Cint($value)
{
    if(!is_numeric($value))
      return 0;
    else
      return (int)$value;
}

 

Sorry about this but I cant get my head around this level of regex.

 

Desmond.

 

Link to comment
Share on other sites

Not interested in wading through all of that code, especially when I have a two line function that will validate an email format:

 

function is_email($email)
{
    $formatTest = '/^[\w!#$%&\'*+\-\/=?^`{|}~]+(\.[\w!#$%&\'*+\-\/=?^`{|}~]+)*@[a-z\d]([a-z\d-]*[a-z\d])?(\.[a-z\d]([a-z\d-]*[a-z\d])?)*\.[a-z]{2,6}$/i';
    $lengthTest = '/^(.{1,64})@(.{4,255})$/';
    return (preg_match($formatTest, $email) && preg_match($lengthTest, $email));
} 

 

Here are the details of the exact validation properties:

// NOTES:
//
// Format test
// - Username:
//     - Can contain the following characters: 
//         - Uppercase and lowercase English letters (a-z, A-Z) 
//         - Digits 0 to 9 
//         - Characters _ ! # $ % & ' * + - / = ? ^ ` { | } ~ 
//     - May contain '.' (periods), but cannot begin or end with a period
//       and they may not appear in succession (i.e. 2 or more in a row) 
//     - Must be between 1 and 64 characters 
// - Domain:
//     - Can contain the following characters: 'a-z', 'A-Z', '0-9', and '-' (hyphen). 
//     - There may be subdomains, separated by a period (.), but the combined domain may not begin with a period
//       and they not appear in succession (i.e. 2 or more in a row) 
//     - Hostname parts may not begin or end with a hyphen 
// - TLD accepts: 'a-z' & 'A-Z'
//
// Note: the domain and tld parts must be between 4 and 256 characters total 
//
// Length test
// - Username: 1 to 64 characters
// - Domain: 4 to 255 character

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.