Hi every one
my question is that i want to validate the form when any one wants to login or sign Up i want to check that the email address is correct or not means that email address include '@' char or not if not an error message should be displayed other wise not. i write the script but the problem is that it show alert message on each char, i want it will alert only one time here the script which i wrote.

for(var i = 0; i < email.length ; i++)
	{
		if(email.indexOf[i] == "@" )
		returnValue = true ;
		
		else
		{
			returnValue = false;
			alert("This is not a valid Email address, Please try again.")}
		}

waiting for your reply with script

Recommended Answers

All 3 Replies

The best way to validate e-mail address is with regular expression.

Here's the one I use:

// returns true or false
function isEmail(str)
{
	var regExp = /\b([\w-\.]+\@([\w-]+\.)+[\da-zA-Z]{2,4})\b/;
	regExp.test(str);
}

Hope it helps.

An easy way to check is using regular expression. Though it is not really a complete validation for email.

if (email.match(/@/)) { return true }

What you may want is to complete the regular expression for an email. However, it is difficult to create a really complete one nowadays.

Edit: You could use the whole regEx from Alemonteiro above post and replace in the match() function. However, remember that there can always be a way to break the rule and get it pass or fail with real/fake email address. One way to do is to split the incoming string into 2 parts - address and domain (address@domain) - using @ as delimiter. Then check each portion for proper patterns as you wish.

Thanks for your reply

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.