|
Posted by Nuno Pereira on 09/16/05 19:11
bruce wrote:
> hi..
>
> looking for a good/working/tested php email validation regex that conforms
> to the rfc2822 standard.
>
> a lot of what i've seen from google breaks, or doesn't follow the standard!
>
> any ideas/thoughts/sample code/etc...
I use this code to build the regex
$tld='[a-z]{2,}';
$regexp='^([_a-z0-9-]+)(\.[_a-z0-9-]+)*'.
'@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.'.$tld.')$';
And test it like this:
/* BEGIN PHP code*/
if (eregi($regexp, $email))
{
list($username,$domaintld) = split("@",$email);
// Validate domain
if (getmxrr($domaintld,$mxrecords)) {
$valid = true;
/*print("Domain: $domaintld<br />");
foreach($mxrecords as $mxKey => $mxValue)
print(" $mxValue<br />");*/
} else {
if (checkdnsrr($domaintld, "any"))
$valid=true;
else
$errors.="The domain ('$domaintld') is invalid.".
" Please check the email.<br />";
}
} else {
$errors.="The email ('$email') isn't valid.<br />";
$valid = false;
}
/* END PHP code*/
I used to get TLD list from
http://data.iana.org/TLD/tlds-alpha-by-domain.txt
Replacing the above TLD with
/*$tld =
'(AC|AD|AE|AERO|AF|AG|AI|AL|AM|AN|AO|AQ|AR|ARPA|AS|AT|AU|AW|AZ|'.
'BA|BB|BD|BE|BF|BG|BH|BI|BIZ|BJ|BM|BN|BO|BR|BS|BT|BV|BW|BY|BZ|CA|CC|'. 'CD|CF|CG|CH|CI|CK|CL|CM|CN|CO|COM|COOP|CR|CU|CV|CX|CY|CZ|DE|DJ|DK|DM'.
'|DO|DZ|EC|EDU|EE|EG|ER|ES|ET|EU|FI|FJ|FK|FM|FO|FR|GA|GB|GD|GE|GF|GG|GH|'.
'GI|GL|GM|GN|GOV|GP|GQ|GR|GS|GT|GU|GW|GY|HK|HM|HN|HR|HT|HU|ID|IE|IL|'. 'IM|IN|INFO|INT|IO|IQ|IR|IS|IT|JE|JM|JO|JP|KE|KG|KH|KI|KM|KN|KR|KW|KY'.
'|KZ|LA|LB|LC|LI|LK|LR|LS|LT|LU|LV|LY|MA|MC|MD|MG|MH|MIL|MK|ML|MM|MN|'. 'MO|MP|MQ|MR|MS|MT|MU|MUSEUM|MV|MW|MX|MY|MZ|NA|NAME|NC|NE|NET|NF|NG|'.
'NI|NL|NO|NP|NR|NU|NZ|OM|ORG|PA|PE|PF|PG|PH|PK|PL|PM|PN|PR|PRO|PS|PT|'. 'PW|PY|QA|RE|RO|RU|RW|SA|SB|SC|SD|SE|SG|SH|SI|SJ|SK|SL|SM|SN|SO|SR|ST'.
'|SU|SV|SY|SZ|TC|TD|TF|TG|TH|TJ|TK|TL|TM|TN|TO|TP|TR|TT|TV|TW|TZ|UA|'.
'UG|UK|UM|US|UY|UZ|VA|VC|VE|VG|VI|VN|VU|WF|WS|YE|YT|YU|ZA|ZM|ZW)';
*/
But as I check if the email domain part is exists, this is not needed.
This is fairly good, and is adapted from a page that i don't have here
the reference. I think that it is RFC compatible in the regex, cause I
remember to see it to build the regex. The domain check code is adapted
as the in the original code it only checks for MX records, but in the
RFC says that if no MX records exist (where he should deliver), try to
deliver to the machine, e.g., if no mail.isp.com MX records exist, try
to deliver the message to the mail.isp.com machine (with SMTP, obviously).
PS: your Perl solution can be good, but I think that there exist PHP
classes that do it, but I don't know one.
--
Nuno Pereira
[Back to original message]
|