Click to See Complete Forum and Search --> : Form Validation


sphinx3212000
08-09-2003, 06:55 PM
For some reason im not getting any of the alerts to come up for my form validation when i try to submit this form...here is the code...any help would be appreciated:

<html>
<head>
<title>Basic Form Validation</title>
<script>
<!--
function validate ()
{
if (document.forms.order.CustomerName.value == " ") {
alert("Please enter your name.")
return false;
}

if (documents.forms.order.CustomerID.value == " ") {
alert("Please enter your Customer ID.")
return false;
}

if (document.forms.order.Qty.value <= 0) {
alert("Please enter a positive number of gadgets.")
return false;
}

return true;
}
// -->

</script>
</head>

<body>
<h1 align="center">Gadget Order Form</h1>
<hr>
<form name="order" method="POST" action="mailto:johndoe@aol.com" onsubmit="return validate()">

<b>Customer Name: </b>
<input type="text" name="CustomerName" id="CustomerName" size="25" maxlength="35">

<br><br>

<b>Customer ID:</b>
<input type="password" name="CustomerID" id="CustomerID" size="8" maxlength="8">

<br><br>

<b>Quantitiy of Gadgets:</b>
<input type="text" name="Qty" id="Qty" size="2" maxlength="2">

<input type="submit" value="order">
<input type="reset" value="reset">

</form>
</body>
</html>

Exuro
08-09-2003, 07:01 PM
Your script works... But it's checking to see if the user has entered a space into the field. I think what you were wanting was to check if the field was empty though... So, what you need to do is take out the space between your quotation marks in your If statements. So then your code would look something like this:

function validate ()
{
if (document.forms.order.CustomerName.value == "") {
alert("Please enter your name.")
return false;
}

if (documents.forms.order.CustomerID.value == "") {
alert("Please enter your Customer ID.")
return false;
}

if (document.forms.order.Qty.value <= 0) {
alert("Please enter a positive number of gadgets.")
return false;
}

return true;
}

I hope that helps!