[thelist] Email Validator 2

Ben Gustafson ben_gustafson at lionbridge.com
Thu Dec 6 11:44:20 CST 2001


This is slightly OT, but here's a function I use for cursory validation of e-mail
addresses. I find it easier than trying to figure out a regular expression (although I'm
sure someone out there could come up with one that does the same thing real quick). It
would be best used in conjunction with self-validation, such as by sending a password to
the address.

--
function isEmail(email) 
{
	var strEmail = trim(email.value);
	// there is only one @ symbol
	if (onlyOneAtSymbol(strEmail) &&
		// the @ symbol's not the first character
		strEmail.indexOf("@") > 0 &&
		// there's a . after the @, and at least one char between it and the @
	   	(strEmail.indexOf(".", strEmail.indexOf("@")) > strEmail.indexOf("@") + 1) && 
	        // there are characters following the .
		strEmail.length > strEmail.indexOf("."))
		return true;
	return false;
	
	// function checks whether there is more than one @ in the string:
	function onlyOneAtSymbol(strEmail) 
	{
		// position of first @
		var firstAt = strEmail.indexOf("@");
		//position of any other @'s
		var otherAt = strEmail.indexOf("@", firstAt + 1);
		// test for only one @
		if (firstAt != -1 && otherAt == -1)
			return true;
		return false;
	}
}
--

--Ben

________________
Ben Gustafson
Lionbridge Technologies, Inc.
www.lionbridge.com




More information about the thelist mailing list