[thelist] Email Validator

Lachlan Cannon tiedefenderdelta6 at yahoo.com
Wed Dec 5 06:56:07 CST 2001


The other day I asked if anyone had their own e-mail
validators (javascript) better than doing an indexOf('@')
and indexOf('.'), and received no replies about that, so I
went to make my own one, this one will only work in
javascript 1.2 and up, but most people would have at least
that these days I'd assume. Anyway, without further ado
here is my solution to the problem (it can be spoofed, but
it's more difficult than the other one. I've checked it and
it worked in IE6, NS6, and Op5, if anyone finds a bug or
has an improvement, please tell me)

<tip type="javascript email validation">
Regular expressions may be ugly, but they do what they do
pretty well. If you want a better email validation script
for javascript than checking for the presence of @ and .,
then the following may help you out.

var pattern=/(^[
\-_\.a-zA-Z0-9]+)@((([0-9]{1,3}\.){3}([0-9]{1,3})((:[0-9])*))|(([a-zA-Z0-9\-]+)(\.[a-zA-Z]{2,})+(\.[a-zA-Z]{2})?((:[0-9])*)))/;

if (email == ""){
	alert("You must supply an address in the \'to\' field.");
}
else if (email.search(pattern) == -1) {
	alert("You must supply only valid e-mail addresses.");
}
</tip>

Lachlan Cannon
{Luminosity}

__________________________________________________
Do You Yahoo!?
Buy the perfect holiday gifts at Yahoo! Shopping.
http://shopping.yahoo.com




More information about the thelist mailing list