js validate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • halyfax
    Senior Member
    • Mar 2004
    • 124

    #1

    js validate

    I have a form and have mutiple checkboxes. Is there a way to force the
    user to check at least 2 checkboxes from a long list? If they only check one it will not let them submit but gives an error?
  • retro
    Member
    • Jun 2004
    • 40

    #2
    I use a script from http://jsval.fantastic-bits.de to validate everything I haven't used it for checkboxes yet so I'm not positive if it works on them or not. Worth a try though.
    Denny Cave
    http://www.retrointeractive.com

    Comment

    • halyfax
      Senior Member
      • Mar 2004
      • 124

      #3
      got it

      this javascript goes into the body section of the html page

      <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
      <!--
      function CountBoxes() {
      var Count = 0
      if (document.test.cb1.checked)
      {Count = Count + 1}
      if (document.test.cb2.checked)
      {Count = Count + 1}
      if (document.test.cb3.checked)
      {Count = Count + 1}

      if (Count < 2)
      {
      alert('Choose at least 2 items')
      document.test; return false;
      }
      }
      // -->
      </script>

      this is how the form with the checkboxes could look like. make sure the name of the form (in this example "test") and the names of the checkboxes are matching with the ones used in the javascript

      <FORM NAME="test" ACTION="" METHOD="POST" ONSUBMIT="return CountBoxes()">
      <INPUT NAME="cb1" TYPE="checkbox" VALUE="blah1"><br>
      <INPUT NAME="cb2" TYPE="checkbox" VALUE="blah2"><br>
      <INPUT NAME="cb3" TYPE="checkbox" VALUE="blah2"><br>
      <INPUT NAME="submit" TYPE="submit" VALUE="Submit">
      </FORM>

      Comment

      Working...