Skip Navigation

Email Address Validation Updated

Background

In June 2004, I posted an article titled Email Address Validation, which included a function for validating email addresses according to RFC 2822 and related documents. It tested only for technical validity, nothing more.

The post proved to be reasonably popular, generating a lot of comments and additions from other PHP developers. Unfortunately, I was not too hot at keeping up, and many improvments took too long to make it to the posted version of the function.

Why Revisit This?

There is still a need for validation of email addresses according to standards. ICANN's approval of their new gLTD program means that older email validation systems that checked for TLDs of 6 characters or fewer are now effectively broken. Domains, and as a result email validation, will be getting much more complicated.

So What's New?

I've incorporated suggestings from other developers who commented on the original article, and as a result it now validates email addresses much more accurately.

I've converted the function to a pretty basic object, which I hope will make it easier to read, understand, modify and also integrate into other systems. The domain validation could also be stripped out of the class easily for use as a domain name validator.

Next, I've created unit tests (PHPUnit) so the validation can be checked as it is changed. More cases need to be added, but it should cover all the basic items that should be allwed and disallowed. Of course, anyone can run this against their own validator(s) if they need to.

Finally, I've released the code under a New BSD License (similar to GPL but permits commercial use) on Google Code - php-email-address-validation/ - and I hope this allows other developers to add to and improve the existing code (suggestions and improvements can be left here or posted to the Google Group).

10 comments

Morgauo
United States #1: July 15, 2008
Great! I very much liked the first version and am looking forward to checking this one out. I apreciate that your code checks all posible address per the RFC, ip addresses / spaces / etc rather than the simple textnospace@textnospace.tld that most validators use. I translated the first version to javascript so that I could do client side validation and save the user a page load. (Always double-check server side!) I'll probably do that again and post it when I get around to it.
the unmodified validator returns the following error message (php 4.4.8, safe_mode enabled):
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in EmailAddressValidator.php on line 27
Hi josh. I've not tested it with PHP4, just PHP5. You're very welcome to adapt it for PHP4 if you like - just let me know and I'll add you to the project so you can upload code.
Rich
United States #4: July 17, 2008
The GNU GPL doesn't prohibit commercial use. If it did, companies like RedHat wouldn't exist.
@Rich: This is true, but the GPL restricts the terms of use more than BSD New (as I understand it). The difference is that with the BSD license a company can use this code commercially, and without attribution.

One down-side of that is that the BSD license allows proprietary knockoffs. People can sell this code, though they probably shouldn't. Once I took the step to release the code, I had to decide what license to best release it under, and the least restrictive made sense.

Future releases may be different - this is the first time I've ever done something like this. For now, I'm happy keeping things as open as possible and seeing how it goes.
Gualtiero
Italy #6: July 26, 2008
Thanks a lo for this useful code!
How do I get the source code for the updated PHP email validation script?
Hi Martin. You need to go to Google Code (the link is in the post), and click on "source". Now, you can either use Subversion (recommended) - the URL will be on the source page - or you can click on "Browse" at the top to view the source online (you'll find the code in the "trunk" folder).
Jens Thomas
Germany #9: August 7, 2008
Thanks for the validation script. Unfortunately it will find an email address like

hello@asd.c

valid, which is wrong from my point of view?! As far a I am concerned there should be a test which check whether the last part of the domain portion is within 2 to 4.
Hi Dave,
Fantastic tool for its purpose. I am having one issue though. Is there any way you could custom license this for me asa GPL 2 version. I am going to integrate it to another GPL 2 platform and I am having license compatibility issue as the platform only accepts code that is GPL 2 licensed.

- Tim

Post Your Comment

· Comments with keywords instead of a name have their URLs removed.
· Your email address will not be displayed or shared.

Live Comment Preview

 United States #11: 1 minute ago