JS e-mail Validator 19 Nov, 2009 JS e-mail Validator

NOTE: I have avoided character classes to aid understanding for newbies. Wizards, bear with the verbosity!

Came up with this nice little email validator JS regexp. Someone got something shorter?

function validateEmail( email ){
var emailRe = new RegExp("^[a-zA-Z]+([a-zA-Z0-9_]*.[a-zA-Z0-9_]+)*@([a-zA-Z0-9_]+.[a-zA-Z0-9_]+)*$",'i');
return( emailRe.test( email ) );
}

Does this miss any cases? Try it out here:

Update: This script allowed '*' in the email ID. The correct re would be:

var emailRe = new RegExp("^[a-zA-Z]+([a-zA-Z0-9].[a-zA-Z0-9]+)+@([a-zA-Z0-9]+.[a-zA-Z0-9]+)+$",'i');

Bug again! The '.' character outside the character class [] matches ANY single character. This, however, definitely works!

var emailRe = new RegExp("^[a-zA-Z]+([a-zA-Z0-9.][a-zA-Z0-9]+)+@([a-zA-Z0-9]+([.a-zA-Z0-9_])+)$".'i');
The test code now does not permit '*' in the email ID.



Tags  ·   javascript  ·   Show Comments ▾


     
Original design for Tumblr crafted by Prashanth Kamalakanthan.
Adapted for Tumblr & Jekyll by Sai Charan. Customized theme available on Github.

Sai Charan's blog by Sai Charan is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Creative Commons License