javascript validate email
Alpesh Mansata June 26th, 2009
function validateEmail(email) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9\-\.])+\.([A-Za-z]{2,4})$/;
if(reg.test(email) == false) {
return false;
}
return true;
}





Very cool!

I think there is one bug in the code.. The regex considers “_” in the URL portion as valid email address, though as per URL RFC1738 (http://www.faqs.org/rfcs/rfc1738.html – pls see Section 2.1) valid characters for a URL does not include “_” (underscore).. And I think, I have never seen any URL with underscore in it.. so, email id with an underscore in domain part will always be invalid.
This occured to me just because I’d reported this in one of my products
Thanks Jigar, fixed it.