Jump to content

email validate script


tronicsmasta

Recommended Posts

Hey guys,

 

found this code online and was wondering how to check the $isValid variable for true or false...

 

I need to validate the users email

 

basically i am using this format:

 

if (!$_POST['email']) {
echo "email field is empty";
$submitform == "no";} 

 

how can I apply that same logic to the output of this code:

 

/**
Validate an email address.
Provide email address (raw input)
Returns true if the email address has the email 
address format and the domain exists.
*/
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   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 = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      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 = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || 
checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

 

thanks!

Link to comment
https://forums.phpfreaks.com/topic/104986-email-validate-script/
Share on other sites

Well the real regular expression for email addresses is like 14 pages long (a very slight exaggeration), but there are plenty out there that work just fine.

 

Google gave me this link and it looks quite decent:

http://www.regular-expressions.info/email.html

 

Anyway, using the regex from there:

 


$email = '[email protected]'; //an example email address

if(preg_match('^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$', $email)) {
    echo 'valid!';
}
else {
     echo 'invalid!';
}

//or function form:

function is_email($email) {
     return preg_match('', $email);
}

if(is_email($email)) {
     echo 'valid!';
}
else {
     echo 'invalid!';
}

 

[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.