Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

specific number of checkboxes

Status
Not open for further replies.

Willette

Technical User
Jul 15, 2002
2
US
A client needs a specific number of option boxes checked, namely 25, no more, no less. Since I am not a programmer I need help!
Does anyone have a suggestion on how to handle this page?

Thanks in advance.
 
You can do a circle to estimate.
(Sorry!My English is not very well.)
 
Hi.

Try this code....
Code:
<html>
<head>
<script language=javascript>
  function chkSelected() {
    var min = 3;
    var max = 5;
    var howmany = document.myform.elements.length;
    var nchecked = 0;
    for (var i = 0; i < howmany; i++) {
      if (document.myform.elements[i].checked == true)
        nchecked = nchecked + 1;
    }
    if (nchecked < min) {
      alert(&quot;You have only selected &quot; + nchecked + &quot; item(s).\n\nPlease select at least &quot; + min + &quot; item(s).&quot;);
      return false;
    }
    if (nchecked > max) {
      alert(&quot;You have selected &quot; + nchecked + &quot; item(s).\n\nPlease select a maximum of &quot; + min + &quot; item(s).&quot;);
      return false;
    }
  }
</script>
</head>
<body>
<form name=&quot;myform&quot;>
Checkbox Example<br>
<table width=100%>
<tr>
<td>
  <div align=&quot;left&quot;>
    <p><font size=&quot;1&quot;>
    <input type=&quot;checkbox&quot; value=&quot;WHITE&quot; name=&quot;option|1|TDG11sp&quot;><b><font face=&quot;Arial&quot;>WHITE</font></b><br>
    <input type=&quot;checkbox&quot; value=&quot;BLACK&quot; name=&quot;option|2|TDG11sp&quot;><b><font face=&quot;Arial&quot;>BLACK</font></b><br>
    <input type=&quot;checkbox&quot; value=&quot;LT GREY&quot; name=&quot;option|3|TDG11sp&quot;><b><font face=&quot;Arial&quot;>LT GREY</font></b><br>
    <input type=&quot;checkbox&quot; value=&quot;MED GREY&quot; name=&quot;option|4|TDG11sp&quot;><b><font face=&quot;Arial&quot;>MED GREY</font></b><br>
    <input type=&quot;checkbox&quot; value=&quot;DK GREY&quot; name=&quot;option|5|TDG11sp&quot;><b><font face=&quot;Arial&quot;>DK GREY</font></b><br>
    <input type=&quot;checkbox&quot; value=&quot;LT BLUE&quot; name=&quot;option|6|TDG11sp&quot;><b><font face=&quot;Arial&quot;>LT BLUE</font></b><br>
    <input type=&quot;checkbox&quot; value=&quot;MED BLUE&quot; name=&quot;option|7|TDG11sp&quot;><b><font face=&quot;Arial&quot;>MED BLUE</font></b><br>
    <input type=&quot;checkbox&quot; value=&quot;POST BLUE&quot; name=&quot;option|8|TDG11sp&quot;><b><font face=&quot;Arial&quot;>POST BLUE</font></b><br>
    <input type=&quot;checkbox&quot; value=&quot;ROYAL&quot; name=&quot;option|9|TDG11sp&quot;><b><font face=&quot;Arial&quot;>ROYAL</font></b><br>
    <input type=&quot;checkbox&quot; value=&quot;TEAL&quot; name=&quot;option|10|TDG11sp&quot;><b><font face=&quot;Arial&quot;>TEAL</font></b><br>
    <input type=&quot;checkbox&quot; value=&quot;NAVY&quot; name=&quot;option|11|TDG11sp&quot;><b><font face=&quot;Arial&quot;>NAVY</font></b></font></p>
  </div>
</td>
</tr>
</table>
<div align=center>
<input type=button name=wb_button value=&quot;Submit Colors&quot; onClick=&quot;chkSelected()&quot;>
</div>
</form>
</body>
</html>

Hope this helps,
Jessica
 
One correction though...

Only check this property on input types of checkboxes.
Code:
    for (var i = 0; i < howmany; i++) {
      if (document.myform.elements[i].type = &quot;checkbox&quot; && document.myform.elements[i].checked == true)
        nchecked = nchecked + 1;
    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top