PDA

View Full Version : Page being submitted after error catch



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>

Veronica
04-30-2007, 04:34 PM
You've basically created an if/else statement, but forgot the else. The last line should be


else {return true}

Otherwise, you've told it to return true even if the other conditions are present.

spinweb
04-30-2007, 06:00 PM
should have caught that one... thanks!