Menu

#8 Improved email verification function

v1.0
open
Eventation (8)
5
2011-04-15
2011-04-15
w-bamberger
No

The best function for email address verification I have met so far is the following. It would be nice to use it since we have it.

function verifyEmailAddress ($email)
{
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex) {
return 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
return false;

}
if ($domainLen < 1 || $domainLen > 255) {
// domain part length exceeded
return false;

}
if ($local[0] == '.' || $local[$localLen-1] == '.') {
// local part starts or ends with '.'
return false;

}
if (preg_match('/\\.\\./', $local)) {
// local part has two consecutive dots
return false;

}
if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
// character not valid in domain part
return false;

}
if (preg_match('/\\.\\./', $domain)) {
// domain part has two consecutive dots
return false;

}
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))) {
return false;
}
}
if ( ! (checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")) ) {
// domain not found in DNS
return false;
}
}
return true;
}

Discussion


Log in to post a comment.

MongoDB Logo MongoDB