Form check contents problem
Posted: Tue Jun 02, 2009 8:23 pm
My form checker script:
the form:
If the user doesn't fill out all of the forms, it does show an alert... but after the user closes the window, it submits the form anyway, without allowing the user to change the contents of the form.
How do I make it cancel the submission of the form if the contents are incomplete and throw an alert?
Code: Select all
<script language="JavaScript" type="text/javascript">
<!--
function checkform ( form )
{
if (form.fname.value == "") {
alert( "All fields are required. Please enter your first name and try again." );
form.email.focus();
return false ;
}
if (form.lname.value == "") {
alert( "All fields are required. Please enter your last name and try again." );
form.email.focus();
return false ;
}
if (form.street.value == "") {
alert( "All fields are required. Please enter your street address and try again." );
form.email.focus();
return false ;
}
if (form.zip.value == "") {
alert( "All fields are required. Please enter your zip code and try again." );
form.email.focus();
return false ;
}
if (form.phone.value == "") {
alert( "All fields are required. Please enter your phone number and try again." );
form.email.focus();
return false ;
}
if (form.problem.value == "") {
alert( "All fields are required. Please briefly describe the problem and try again." );
form.email.focus();
return false ;
}
return true ;
}
//-->
</script>
Code: Select all
<form action="insert_appt.php" method="post" onsubmit="return checkform(this);">
First Name:
<input type="text" name="fname" />
Last Name:
<input type="text" name="lname" /> <br><br>
Street Address:
<input type="text" name="street" />
Zip Code:
<input type="text" name="zip" /> <br><br>
Phone Number:
<input type="text" name="phone" /> <br><br>
Briefly Describe the Problem: <br>
<textarea name="problem" rows=6 cols=50></textarea> <br><br>
<input type="hidden" name="id" value="<?php echo "$appt_id"; ?>">
<input type="hidden" name="month" value="<?php echo "$appt_month"; ?>">
<input type="hidden" name="day" value="<?php echo "$appt_day"; ?>">
<input type="hidden" name="year" value="<?php echo "$appt_year"; ?>">
<input type="hidden" name="time" value="<?php echo "$appt_time"; ?>">
<input type="hidden" name="ampm" value="<?php echo "$appt_ampm"; ?>">
<input type="submit" value="Schedule Now!" />
</form>
How do I make it cancel the submission of the form if the contents are incomplete and throw an alert?