Form check contents problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
rbroadwe
Forum Newbie
Posts: 10
Joined: Wed May 06, 2009 5:18 pm

Form check contents problem

Post by rbroadwe »

My form checker script:

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>
 
the form:

Code: Select all

 
<form action="insert_appt.php" method="post" onsubmit="return checkform(this);">
                First Name:
                <input type="text" name="fname" />
                &nbsp; Last Name:
                <input type="text" name="lname" /> <br><br>
                Street Address:
                <input type="text" name="street" />
                &nbsp; 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>
 
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?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Form check contents problem

Post by kaszu »

Problem is "form.email.focus();", which throws error because there is no such field. After error is thrown rest of the function (including "return false;") is not executed, so submit event is not canceled and form is submitted.
Remove "form.email.focus();" or replace it with valid field.
Post Reply