Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validate email with multiple messages 1

Status
Not open for further replies.

tester321

Programmer
Mar 13, 2007
150
CA
Hi i'm using the following to validate a users input box for email. I need to have all the messages associated with each miss-hap but was wondering if there was a more elegant way to accomplish what i have, thanks:
Code:
	function ValidateEmail(strEmail) {
		if(-1 == strEmail.indexOf("@")) { 
			document.all('EMail').focus(); 
			alert("Your email must have a '@'."); 
			return false; 
		}
		if(-1 == strEmail.indexOf(".")) { 
			document.all('EMail').focus(); 
			alert("Your email must have a '.'."); 
			return false; 
		}		
		if(-1 != strEmail.indexOf(",")) { 
			document.all('EMail').focus(); 
			alert("Your email must not have a ',' in it"); 
			return false; 
		}
		if(-1 != strEmail.indexOf("#")) { 
			document.all('EMail').focus(); 
			alert("Your email must not have an '#' in it." ); 
			return false; 
		}
		if(-1 != strEmail.indexOf("!")) { 
			document.all('EMail').focus(); 
			alert("Your email must not have a '!' in it." ); 
			return false; 
		}
		if(-1 != strEmail.indexOf("'")) { 
			document.all('EMail').focus(); 
			alert("Your email must not have a ' in it." ); 
			return false; 
		}
		if(-1 != strEmail.indexOf(""")) { 
			document.all('EMail').focus(); 
			alert("Your email must not have a " in it." ); 
			return false; 
		}
		if(-1 != strEmail.indexOf("%")) { 
			document.all('EMail').focus(); 
			alert("Your email must not have a "%" in it." ); 
			return false; 
		}
		if(-1 != strEmail.indexOf("&")) { 
			document.all('EMail').focus(); 
			alert("Your email must not have a "&" in it." ); 
			return false; 
		}
		if(-1 != strEmail.indexOf("+")) { 
			document.all('EMail').focus(); 
			alert("Your email must not have a "+" in it." ); 
			return false; 
		}
		
		if(-1 != strEmail.indexOf(" ")) { 
			document.all('EMail').focus(); 
			alert("Your email must not have a space in it." ); 
			return false; 
		}
		if(strEmail.length == (strEmail.indexOf("@")+1) ) {
			document.all('EMail').focus();
			alert("Your email must have a domain name after the '@'.");
			return false;
		}
		if(strEmail.length == 0) {
			document.all('EMail').focus();
			return false; 
		}
		return true;
	}
 
You should look at using a regular expression to solve this simple problem. There are plenty of examples here and in the FAQ section.

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Hi

Jeff said:
You should look at using a regular expression to solve this simple problem.
Yes, that is what most of us would do. But I think tester321 just not wants that :
tester321 said:
validate email with multiple messages
And anyway, while he/she is using Explorer-only element references, probably that 68 line function also matches his/her style...

Feherke.
 
So here is a rewrite without using regular expressions that is a little easier to maintain:
Code:
function ValidateEmail(strEmail)
{
	var illegalCharacters = [ ',', '#', '!', "'", '"', '%', '&', '+', ' ' ];
	var requiredCharacters = [ '.', '@' ];
	var message = "";
	for (var loop=0, max=illegalCharacters.length; loop < max; loop++)
	{
		if (-1 != strEmail.indexOf(illegalCharacters[loop]))
		{
			message = "Your email must not have a '" + illegalCharacters[loop] + "' in it.";
			break;
		}
	}
	if (message == "")
	{
		for (var loop=0, max=requiredCharacters.length; loop < max; loop++)
		{
			if (-1 == strEmail.indexOf(requiredCharacters[loop]))
			{
				message = "Your email must have a '" + requiredCharacters[loop] + "'.";
				break;
			}
		}
	}
	if (message == "" && strEmail.length == (strEmail.indexOf("@")+1))
	{
		message = "Your email must have a domain name after the '@'.";
	}
	if (message == "" && strEmail.length == 0)
	{
		message = "You must supply an email address";
	}
	if (message == "")
	{
		return true;
	} else {
		if (document.all)
		{
			document.all('EMail').focus();
		}
		alert(message);
		return false;
	}
}

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top