Author Topic: [SOLVED] Validating email address?  (Read 253 times)

0 Members and 1 Guest are viewing this topic.

Offline galvinTopic starter

  • Devotee
  • Posts: 520
    • View Profile
[SOLVED] Validating email address?
« on: June 13, 2009, 09:44:57 PM »
I got this function online to validate an email address and I'm having trouble.  All the code is below but when I actually try to check to see if the function validEmail returns $isValid as true or false, I am getting false always (even when I enter perfectly good email addresses).

This is how I am calling the function and checking to see if $isValid is true or false.   My guess is that Im doing something stupid here.  My form has an email field with name 'email' so that is what I'm checking below obviously.  I keep getting "Email address is not valid"....

Code: [Select]
validEmail($_POST['email']);

if (!$isValid) {
$bademail = "Email Address is not valid";
} else  ($isValid) {
$bademail = "email is fine";
}


This is the actual validEmail function I got online...

Code: [Select]

/**
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 && !(myCheckDNSRR($domain,"MX") || myCheckDNSRR($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}


function myCheckDNSRR($hostName, $recType = '')
{
if(!empty($hostName)) {
if( $recType == '' ) $recType = "MX";
exec("nslookup -type=$recType $hostName", $result);
// check each line to find the one that starts with the host
// name. If it exists then the function succeeded.
foreach ($result as $line) {
if(eregi("^$hostName",$line)) {
return true;
}
}
// otherwise there was no mail handler for the domain
return false;
}
return false;
}

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Validating email address?
« Reply #1 on: June 14, 2009, 05:14:13 AM »
$isValid does not exist outside of the function.

Either do....


$isValid 
validEmail($_POST['email']);

if (!
$isValid) {
  
$bademail "Email Address is not valid";
} else {
   
$bademail "email is fine";
}

echo 
$bademail;


or the more common....


if (validEmail($_POST['email'])) {
  echo 
"Email Address is not valid";
} else {
  echo 
"email is fine";
}

Offline galvinTopic starter

  • Devotee
  • Posts: 520
    • View Profile
Re: Validating email address?
« Reply #2 on: July 02, 2009, 10:10:46 AM »
Awesome, thanks!