php - Using a regular expression to validate an email address -
over years have developed regular expression validates email addresses correctly, assuming don't utilize ip address server part.
i utilize in several php programs, , works of time. however, time time contacted having problem site uses it, , end having create adjustment (most realized wasn't allowing 4-character tlds).
what's best regular look have or have seen validating emails?
i've seen several solutions utilize functions utilize several shorter expressions, i'd rather have 1 long complex look in simple function instead of several short look in more complex function.
there no simple regular look problem: see this rfc‑822–compliant regex, simple. (it written before days of grammatical patterns.) grammar specified in rfc 5322 complicated primitive regular expressions.
the more sophisticated grammatical patterns in perl, pcre, , php can manage correctly parse rfc 5322 without hitch. python , c# should able manage it, utilize different syntax first three. however, if forced utilize 1 of many less powerful pattern-matching languages, it’s best utilize real parser.
it's of import understand validating per rfc tells absolutely nil whether address exists @ supplied domain, or whether person entering address true owner. people sign others mailing lists way time. fixing requires fancier kind of validation involves sending address message includes confirmation token meant entered in same web page address.
confirmation tokens way know got address of person entering it. why mailing lists utilize mechanism confirm sign-ups. after all, can set downwards president@whitehouse.gov
, , parse legal, isn't person @ other end.
for php, should not utilize pattern given in validate e-mail address php, right way quote:
there danger mutual usage , widespread sloppy coding found de facto standard e-mail addresses more restrictive recorded formal standard.
that no improve other non-rfc patterns. isn’t smart plenty handle rfc 822, allow lone rfc 5322. this one, however, is.
if want fancy , pedantic, implement finish state engine. regular look can deed rudimentary filter. problem regular expressions telling valid e-mail address invalid (a false positive) because regular look can't handle rude , impolite user's perspective. state engine purpose can both validate , right e-mail addresses otherwise considered invalid disassembles e-mail address according each rfc. allows potentially more pleasing experience, like
the specified e-mail address 'myemail@address,com' invalid. did mean 'myemail@address.com'?
see validating email addresses, including comments. or comparing e-mail address validating regular expressions.
debuggex demo
php regex email email-validation