Is there a php library for email address validation?
I need to validate the email address of my users. Unfortunately, making a validator that comforms to standards is hard
Here is an example of a regex expression that tries to comform to standard
Are there any PHP library (preferably, open-source) that validates email address?
Have you looked at PHP's filter_ functions? They're not perfect, but they do a fairly decent job in my experience.
Example usage (returns boolean):
filter_var($someEmail, FILTER_VALIDATE_EMAIL);
AFAIK, the only good way to validate an e-mail is to to send an e-mail and see if user goes back to the site using a link in this e-mail. That's what lot of sites do.
As you point out with the link to the well known mammoth regex, validating all forms of e-mail address is hard, near to impossible. It is so easy to do it wrong, even for trivial style e-mails (I found too many sites rejecting caps in e-mail addresses! And most old regexes reject TLDs of more than 4 letters!).
AFAIK, "Jean-Luc B. O'Grady"@example.com and e=mc^2@[82.128.45.117] are both valid addresses... While I-Made-It-Up@Absurd-Domain-Name.info is likely to be invalid.
So somehow, I would just check that we have something, a unique @, something else, and go with it: it would catch most user errors (like empty field or user name instead of e-mail address).
If user wants to give a fake address, it would just give something random looking correct (see@on.tv or bill.gates@microsoft.com). And no validator will catch typos (jhon.b@example.com instead of john.b@example.com).
If one really want to validate e-mails against full RFC, I would advise to use regexes to split around @, then check separately local name and domain name. Separate case of local name starting with " from other cases, etc. Separate case of domain name starting with [ from other cases, etc. Split problem in smaller specific domains, and use regexes only on a well defined, simpler cases.
This advice can be applied to lot of regex uses, of course...
[UPDATED] I've collated everything I know about email address validation here: http://isemail.info, which now not only validates but also diagnoses problems with email addresses. I agree with many of the comments here that validation is only part of the answer; see my essay at http://isemail.info/about.
I've now collated test cases from Cal Henderson, Dave Child, Phil Haack, Doug Lovell and RFC 3696. 158 test addresses in all.
I ran all these tests against all the validators I could find. The comparison is here: http://www.dominicsayers.com/isemail
I'll try to keep this page up-to-date as people enhance their validators. Thanks to Cal, Dave and Phil for their help and co-operation in compiling these tests and constructive criticism of my own validator.
People should be aware of the errata against RFC 3696 in particular. Three of the canonical examples are in fact invalid addresses. And the maximum length of an address is 254 or 256 characters, not 320.
链接地址: http://www.djcxy.com/p/2706.html上一篇: 正则表达式很难识别电子邮件地址?
下一篇: 有一个用于电子邮件地址验证的PHP库吗?