Click to See Complete Forum and Search --> : Stoping form from submitting


Arc
10-17-2003, 05:04 PM
Hi. I am wondering how to stop a form from being submitted once an error has been raised. I have a Validate function that gives an error if any of the fields aren't correct. The problem is the form submits even if there is an error.

I am using the OnSubmit of the form tag like so

<form name="form1" method="post" action="addlog2.php" onSubmit="Validate()">

and my Validate function looks like so


<script language="JavaScript" type="text/JavaScript">
function Validate(){
var errormsg = "Please make these corrections.";
if(document.form.selCategory.value == "*Select Category*") errormsg += "\n -Select a Category.";
if(document.form.txtCustomer.value == "") errormsg += "\n -Enter a Customer Name.";
if(document.form.txtDeal.value == "") errormsg += "\n -Enter a Deal Number.";
if(document.form.txtStockNum.value == "") errormsg += "\n -Enter a Stock Number.";
if(isNaN(document.form.txtYear.value) || document.form.txtYear.value.Length<4) errormsg += "\n -Enter a valid Year i.e.(1998).";
if(document.form.txtMake.value == "") errormsg += "\n -Enter a Make.";
if(document.form.txtSalesman1.value == "") errormsg += "\n -Enter a name for Salesman1.";
if(document.form.txtSource.value == "") errormsg += "\n -Enter a Source.";
if(isNaN(document.form.txtFinance.value) || document.form.txtFinance.value=="") errormsg += "\n -Enter a valid Finance Income amount. i.e. (1000)";
if(isNaN(document.form.txtVSC.value) || document.form.txtVSC.value=="") errormsg += "\n -Enter a valid VSC Income amount. i.e. (1000)";
if(isNaN(document.form.txtLife.value) || document.form.txtLife.value=="") errormsg += "\n -Enter a valid Life Income amount. i.e. (1000)";
if(isNaN(document.form.txtDisability.value) || document.form.txtDisability.value=="") errormsg += "\n -Enter a valid Disability Income amount. i.e. (1000)";
if(isNaN(document.form.txtGAP.value) || document.form.txtGAP.value=="") errormsg += "\n -Enter a valid GAP Income amount. i.e. (1000)";
if(isNaN(document.form.txtETCH.value) || document.form.txtETCH.value=="") errormsg += "\n -Enter a valid YETCH Income amount. i.e. (1000)";
if(document.form.txtFandI.value == "") errormsg += "\n -Enter a F&I Manager name.";
if(document.form.txtSales.value == "") errormsg += "\n -Enter a Sales Manager name.";

if(errormsg != "Please make these corrections."){
alert(errormsg);
}
}//end function

//-->
</script>


Is there anything I can put in that function to tell the form to not submit?

If I use the Submit buttons OnMouseDown event I will get the error message and the form won;t submit. But the problem with that is the user can still use the Enter button on the keyboard to submit the form.

Thanks!:D

Arc
10-17-2003, 06:36 PM
nm I figured it out.

Charles
10-17-2003, 06:40 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>

<script type="text/javascript">
<!--
function require(form) {for (var i=0; i<form.elements.length; i++) {if (form.elements[i].type == 'text' && !/\S/.test(form.elements[i].value)) {alert(['Field', form.elements[i].previousSibling.data, 'is required.'].join(' ')); form.elements[i].focus(); return false}}}
// -->
</script>

<form action="" onsubmit="return require(this)">
<div>
<label>Fee<input type="text"></label>
<label>Fie<input type="text"></label>
<label>Foe<input type="text"></label>
<label>Fum<input type="text"></label>
<button type="submit">Submit</button>
</div>
</form>