Loading ...
Sorry, an error occurred while loading the content.

Re: Validating a form...

Expand Messages
  • Jon Stephens
    Are all your form elements inside a form element? The onsubmit handler is an attribute of the form tag, not the input. function
    Message 1 of 2 , Feb 1, 2005
    • 0 Attachment
      Are all your form elements inside a form element?

      The onsubmit handler is an attribute of the form tag, not the input.

      <script type="text/javascript">

      function checkFields(form)
      {
      var atLeastOne = form.daytime.selectedIndex
      && form.otherCertificate.selectedIndex
      && form.diplomaProgram.selectedIndex;

      if(!atLeastOne)
      alert("You must select at least one of the options.");

      return atLeastOne;
      }

      </script>

      <form name="info" onsubmit="return checkFields(this);">
      <select name="daytime">
      <option selected="selected">--choose one--</option>
      <!-- more options -->
      </select>
      <select name="otherCertificate">
      <option selected="selected">--choose one--</option>
      <!-- more options -->
      </select>
      <select name="diplomaProgram">
      <option selected="selected">--choose one--</option>
      <!-- more options -->
      </select>
      <input name="submit" type="submit"
      onsubmit="return checkFields();" value="Apply" />
      </form>

      > Date: Sat, 29 Jan 2005 20:50:22 -0000
      > From: "howievinnie" <howievinnie@...>
      > Subject: Validating a form...
      >
      >
      >
      > Hi, I have a form with three drop down boxes. The user must choose
      > from one of the three. I've started with the following function:
      >
      > function checkFields() {
      > Daytime = document.info.Daytime.selectedIndex;
      > Other_Certificate = document.info.Other_Certificate.selectedIndex;
      > Diploma_Prog = document.info.Diploma_Prog.selectedIndex;
      >
      >
      > if ((document.info.Daytime.selectedIndex == 0) &&
      > (document.info.Other_Certificate.selectedIndex == 0) &&
      > (document.info.Diploma_Prog.selectedIndex == 0) ) {
      > alert("You need to select an option.");
      > return false;
      > }
      > else return true;
      > }
      >
      > This is the start of one of the dropdown boxes:
      >
      > <select name="Daytime">
      > <option selected="selected"></option>
      > <option>General Management 52 weeks, discount
      > payment now
      > $11995</option>
      > <option>Hotel/Restaurant Management 39 weeks,
      > discount payment
      > now $9995</option>
      >
      > This is the submit button:
      >
      > <input name="submit" type="submit" onSubmit="return checkFields()
      > value="Apply" />
      >
      > Why can't I get it to work?
      >
      > Thanks
    Your message has been successfully submitted and would be delivered to recipients shortly.