Regex: validate e-mail address in PHP
Validating whether an e-mail address conforms to the (informal) e-mail address specification is a good way of improving the overall validity of the information entered by a user. Regular expressions can help us in accomplishing this.
The following example doesn’t follow the RFC 2822 specification per se but enforces the more strict rules which are common for the internet nowadays and for instance enforced by Live Hotmail and Gmail.
The Wikipedia article about e-mail addresses is somewhat easier to digest than the original specification and is a good starting point for e-mail validation techniques.
Here goes!
Anatomy for i.e. john@doe.example.com:
- john is the local name
- doe is the subdomain name
- example is the domain name
- com is the top-level domain name
The rules:
- Local name may only contain letters, digits, hyphen ‘-’, underscore ‘_’ and periods ‘.’
- Local name may not begin and/or end with a period
- Local name may not contain two or more subsequent periods ‘..’
- Sub-domain name may only contain letters, digits and hyphen
- Multiple sub-domains are permitted
- Domain name may only contain letters, digits and hyphen
- Domain name and sub-domain name may not begin and/or end with a hyphen
- Domain name and sub-domain must be between 2 to 63 characters long
- Top-level domain may only contain letters
- Top-level domain must be between 2 to 6 characters long
- Sub-domain names, the domain name and the top-level domain name are separated by single periods ‘.’
The regular expression (Perl compatible):
/^[A-z0-9\-_]+(\.[A-z0-9\-_]+)*@(([A-z0-9]+\-?[A-z0-9]+)+\.)+[A-z]{2,6}$/
And a PHP function to validate the e-mail addresses:
// Pass the e-mail address to the function. // Will return true when valid, false when invalid. function validate_emailaddress($input) { return preg_match('/^[A-z0-9\-_]+(\.[A-z0-9\-_]+)*@(([A-z0-9]+\-?[A-z0-9]+)+\.)+[A-z]{2,6}$/', $input); }
Note that the solution shown will discount alot of possible RFC 2822 compliant e-mail adressess as invalid but these are mostly used in intranet settings. Stricter rules apply for the web and these are the basis for the expression I’ve made.
What the expression doesn’t account for:
- It doesn’t check whether the subdomain and domain names’ length exceeds the maximum of 63 characters
- Every made up top-level domain will pass, given it is between 2 and 6 characters long
- Something I might have missed?
If you have a solution for the aforementioned shortcomings, please let me know!
That was stimulating . I love your finesse that you put into your writing . Please do move forward with more similar to this.
February 4th, 2010 at 8:59 amThere’s a book known as “Avoid Retirement And Stay Alive”. The notion will be the fact that retirement has no area in current society. When you have the ability to make function pleasant by balancing it against the other points you’d like to do, then you can easily reside like you’ve got all the time within the globe.
July 24th, 2010 at 1:10 pmGood Page, thats Nice Information for me..
July 31st, 2010 at 10:11 pm