I took this further.. the following script will validate each part of the email address one part at a time and just drill down to errors more specifically. It also trims invalid characters, lowercases the string and returns the address in proper format if it is valid. If something is wrong, it returns a number from 1 to 15 based on the error that it finds. I'm sure there are flaws in this and ways to get through it. (Or something that is inaccurate or overlooked)
See if you can find anything.
function validate_email ($email) {
# Trim out 'null' characters
$email = ereg_replace("\t","",$email);
$email = ereg_replace("\r","",$email);
$email = ereg_replace("\n","",$email);
$email = ereg_replace(" ","",$email);
# Email addresses are not case sensitive
$email = strtolower($email);
# Must contain '@' and '.'
if ((!ereg("@",$email)) || (!ereg(".",$email))) {
return "1";
}
# Must be at least 7 characters long (x@y.com)
elseif (strlen($email) < 7) {
return "2";
}
else {
# Split on the @ sign
$parts = split("@", $email, 2);
# First part is user
$user = $parts[0];
# Second part is domain
$domain = $parts[1];
# Domain must contain at least 1 dot.
if (!ereg("\\.",$domain)) {
return "3";
}
# Must be a least 4 characters (z.com)
elseif (strlen($domain) < 4) {
return "4";
}
# May not start with a dot
elseif (ereg("^\\.",$domain)) {
return "5";
}
# May not have more than one dot in sequence
elseif (ereg("\\.\\.",$domain)) {
return "6";
}
else {
# User must be at least 1 character long
if (strlen($user) < 1) {
return "7";
}
# User cannot contain a comma
elseif (ereg("\\,", $user)) {
return "8";
}
else {
# Split on the . character
$parts = split("\\.", $domain);
# There must be at least two parts to a domain (a sub domain would have 3 - a.b.com)
if (count($parts) < 2) {
return "9";
}
else {
# First part is domain name
$name_at = count($parts) - 2;
$name = $parts[$name_at];
# Second part is domain extention
$ext_at = count($parts) - 1;
$ext = $parts[$ext_at];
$first_name = substr($name, 0, 1);
# Name must be at least one character
if (strlen($name) < 1) {
return "10";
}
# Name cannot be more than 26 characters
if (strlen($name) > 26) {
return "11";
}
# Extention must be 2 or 3 characters (.nu - .com)
elseif ((strlen($ext) > 3) || (strlen($ext) < 2)) {
return "12";
}
# Domain can only contain the following characters
elseif (strspn($name, "abcdefghijklmnopqrstuvwxyz0123456789-.") != strlen($name)) {
return "13";
}
# Domain can only start with the following characters
elseif (strspn($first_name, "abcdefghijklmnopqrstuvwxyz") < 1) {
return "14";
}
# Extension can only contain the follow characters
elseif (strspn($ext, "abcdefghijklmnopqrstuvwxyz") != strlen($ext)) {
return "15";
}
else {
return $email;
}
}
}
}
}
}