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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to fire the alert with the submit button?

Status
Not open for further replies.
Oct 11, 2006
300
US
I am unable to fire the alert messages to check if the email is valid or not. secondly with nothing in the textbox when I click on the submit button, the form still gets submitted. How can I prevent this?

Thanks.

Code:
<script type="text/javascript">
function checkEmail(obj)
{
	isEmail = /^([0-9a-z_&.+-]+!)*[0-9a-z_&.+-]+@(([0-9a-z]([0-9a-z-]*[0-9a-z])?\.)+[a-z]{2,3}|([0-9]{1,3}\.){3}[0-9]{1,3})$/i;
	notEmail = /^(((postmaster|root|hostmaster|mailer-daemon|webmaster)@(wyvernwood|frame)\.com)|.*@(.*\ .(wyvernwood|frame)\.com|localhost\.com|127\.0\.0\ .1))$/i;

	if(obj.value.indexOf('@',0) == -1 ||
		obj.value.indexOf('@',0) == 0 ||
		obj.value.indexOf('.',0) == -1)
	{
		errorObj = obj;
		alert("Invalid Email Address.");
		return false;
	}
	else
		if(notEmail.test(obj.value))
			{
				errorObj = obj;
				alert("Invalid Email Address.");
				return false;
			}
	return true;
}

</script>

</head>

<body>
<form id="form1" name="form1" method="post" onSubmit="return checkEmail(this);" action="add_to_plaint.asp">

Email: <input type="text" name="email" size="20">
<input type="submit" name="Submit" value="Submit" />
</form>
 
I realized that the function could not find the obj within the function. So I refered the object to document.form1.email in my function.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top