spinweb
04-30-2007, 02:39 PM
I have a form validation script that seems to be working. However, when it does its job and catches an error, my page still gets submitted.
How to I keep it from being submitted until all fields are correctly entered?
Here's the event handler: <form id="private" method="post" action="contact.php" class="form" onSubmit="return ValidateData(this)">. Script is below.
Thx,
Rick
<script language="javascript">
function ValidateData(form)
{
if (!form.name.value)
{
alert("\nName is a required field.");
form.name.focus();
return (false);
}
var email = form.email.value;
if (!email)
{
alert("\nEmail Address is a required field.");
form.email.focus();
return (false);
}
if ((email.indexOf(" ") > -1) || (email.indexOf("@") == - 1) || (email.indexOf(".") == -1))
{
alert("Please enter a valid Email Address.");
form.email.focus();
return (false);
}
if (!form.message.value)
{
alert("\nMessage is a required field.");
form.message.focus();
return (false);
}
return (true);
}
</script>
How to I keep it from being submitted until all fields are correctly entered?
Here's the event handler: <form id="private" method="post" action="contact.php" class="form" onSubmit="return ValidateData(this)">. Script is below.
Thx,
Rick
<script language="javascript">
function ValidateData(form)
{
if (!form.name.value)
{
alert("\nName is a required field.");
form.name.focus();
return (false);
}
var email = form.email.value;
if (!email)
{
alert("\nEmail Address is a required field.");
form.email.focus();
return (false);
}
if ((email.indexOf(" ") > -1) || (email.indexOf("@") == - 1) || (email.indexOf(".") == -1))
{
alert("Please enter a valid Email Address.");
form.email.focus();
return (false);
}
if (!form.message.value)
{
alert("\nMessage is a required field.");
form.message.focus();
return (false);
}
return (true);
}
</script>