[UPHPU] Email address format in regular expression
John
lists at strictlyrockymountain.com
Wed Dec 28 15:40:39 MST 2005
> Using the ereg() function in PHP
> (http://us2.php.net/manual/en/function.ereg.php), I'm using this lengthy
> expression to determine a properly formatted email address:
>
> ^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$
>
> However, I know very little about regular expressions and don't know if
> this is accurate. It seems to work. Is this the best expression for an
> email address? Is there an *official* format as determined by the email
> protocol?
>
> Many thanks.
>
> -Cabot Nelson
I'm sure there are others better at regexp than I, but here is a function
I use on my sites, and I've had little to no trouble. I did some research
initially before writting this, and found that there needs to be at least
2 characters on the end, thus {2,} and all current root's are all alpha,
and no numerical (com, au, edu, etc). Otherwise, there are strict rules
about what can be in the user and domain portions. And this one below
follows those rules - IMHO.
function validate_email($email)
{
if
(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@([a-z0-9][a-z0-9-]*[a-z0-9]\.)+[a-z]{2,}",
$email))
{
list($user,$domain) = split("@",$email,2);
if ($valid = checkdnsrr($domain,"MX"))
return TRUE;
else
return FALSE;
}
else
return FALSE;
}
The function first validates the email address, and then does a dns MX
record lookup. Doing a DNS MX lookup is much more relative to this
process, than just doing a DNS A or CNAME lookup, because I can have a
domain, without having an mail server. And by that standard, I would not
want to send my customer an email to a domain that does not have mail. So
if the dns lookup fails, then I don't accept it as a valid email address.
However that is where the one time I had a problem, came from. Their dns
was down, and this rejected it. Which was, by the letter of the law,
exactly as it was supposed to work.
--
Thanks
John
More information about the UPHPU
mailing list