javascript regexp email validator
I have been looking for a javascript email regexp email validator. And I found one from this web site. And the code is
"/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i"
This code looks nice.
However, I am not totally satisfied with the code. Because there are emails addresses don't look like "abc@abc.com". For example, I used to send email to an address like "joel@192.168.100.1". So I am going to make some change to the regular expression filter.
Update 1:
"/^([\w-]+(?:\.[\w-]+)*)\@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$|(\[?(\d{1,3}\.){3}\d{1,3}\]?)$/i"
will detect email like "abc@127.0.0.1" or "abc@[127.0.0.1]".
Update 2:
/^([\w-]+(?:\.[\w-]+)*)\@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$|(\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)$/i
Now this one won't let a.b.c@256.0.0.0 slip.
Update 3:
/^(("?)[\w- ]+\1)|([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$|(\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)$/i
Now this regular expression thinks "Guy Joel"@abc.com or "Guy Joel"guyj@abc.com are good email address.
Update 4:
/^(("?)[\w-\s]+\1)|([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$|(\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)$/i
A little bit more update on space in the regexp.
Update 5:
/^("[\w-\s]+")?([\w-]+(?:\.[\w-]+)*)?@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$|(\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)$/i
Made a big mistake in the last one. So, another update.
Update 6:
/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$|(\[?((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)$/i
It's getting longer and longer, better and better.
Update 7:
/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i
The update 6 thinks 256.255.255.255 is a legal ip address, fixed in update 7.
string | result |
joel@joel.com | pass |
joel@joel.edu | pass |
joel@joel.museum | pass |
joel@joel.ac.uk | pass |
joel@guy.joel.ac.uk | pass |
guy.joel@guy.joel.ac.uk | pass |
guy.joel@127.0.0.1 | pass |
guy.joel@[127.0.0.1] | pass |
\"Guy Joel\"guy.joel@[127.0.0.1 | pass |
\"Guy Joel\"@[127.0.0.1] | pass |
\"Guy Joel\"@9.999.99.25 | fail |
\"Guy Joel\"@999.99.99.25 | fail |
"\"Guy Joel\"" | fail |
Also, regular expression for url validation.
Labels: email, javascript, regexp, regular expression
10 Comments:
fails with thiws: name.surname@bla.bla2.dk
I tried "name.surname@bla.bla2.dk", passed.
abc.abc@0.0.0.0 pass. Shouldn't it fail?
@John,
You are right. But the code has become ridiculous, I am not sure if I will work on it.
How about a different approach?
Check for an @ sign, then use ajax and a php script to try to connect to the base host, everything after @, using fsockopen to verify it is a real host.
Then you just need to check the @ sign exists and there is at least 1 character before it and you should be right..
johnm: Good idea. Connecting to the host is unnecessary, though. You could simply do a DNS lookup and verify that the host as an MX record.
@JohnM
Of course you can do that. But the purpose of this piece of code I wrote several years ago was not to catch faked email address. People can always fake email address, I can even register a new one and forget it forever, I actually forgot many of them.
The purpose of this code is to catch input mistakes, and prevent those mistakes from reaching the sever. As a result, the server take less load.
fails with «+» symbol.
test+noreply@blogger.com
This is an interesting problem, thanks for not only "providing a solution", but also showing the though processes associated with each version.
btw, there are some mistakes (I know that you have no intention of making additional changes).
- The square brackets around the IP address, if unbalanced, will still "Pass".
e.g., "me@[120.0.0.1"
- It is my understanding that the stuff in quotes at the beginning may contain more than Letters, digits, underscore, and space. e.g., this should be valid:
"Joseph A. (Joe) Cool"@domain.com
Thanks for sharing.
Hi programmers,
It may helpful to you.
var emailRegex= new RegExp(/^([a-zA-Z0-9]{3,})(((\.|\-|\_)[a-zA-Z0-9]{2,})+)?@([a-z]{3,})(\-[a-z0-9]{3,})?(\.[a-z]{2,})+$/);
Thanks..
Regards,
Web-Farmer
www.letsnurture.com
Post a Comment
<< Home