Solved

Limit the number of check boxes selected

Posted on 2004-04-06
13
885 Views
Last Modified: 2008-03-06


I have a form with 5 checkboxes (could be more) along with other form elements, can you give me a function that constantly (on keypress?) check the number of check boxes being selected and limit them to 3? If a user select more than 3 (the 4th one) then it will pop a message "Please select only 3 options"

Thanks,

Andre

Part of my form code:


--------------------------------

<tr>                                    
      <td>&nbsp;</td>
      <td><input type="checkbox" name="SceneType" value="1">A</td>
</tr>
<tr>                                    
      <td>&nbsp;</td>
      <td><input type="checkbox" name="SceneType" value="2">B</td>
</tr>
<tr>                                    
      <td>&nbsp;</td>
      <td><input type="checkbox" name="SceneType" value="3">C</td>
</tr>
<tr>                                    
      <td>&nbsp;</td>
      <td><input type="checkbox" name="SceneType" value="4">D</td>
</tr>
<tr>                                    
      <td>&nbsp;</td>
      <td><input type="checkbox" name="SceneType" value="5">E</td>
</tr>

--------------------------------
0
Comment
Question by:andreni78
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 33

Assisted Solution

by:sajuks
sajuks earned 100 total points
ID: 10764256
<html>
<head>
<script>
function checkthis(theBox){
 boxName=theBox.name;
 elm=theBox.form.elements;
 count=0;
 for(i=0;i<elm.length;i++)
   if(elm[i].name==boxName && elm[i].checked==true)
     count++
 if(count > 3){
   alert('More then three boxes of the group"'+theBox.name+'" checked.')
   theBox.checked=false;
 }
}
</script>
</head>
<body>
<form>
<input type=checkbox name=SceneType value="1" onClick="checkthis(this)">A
<input type=checkbox name=SceneType value="2" onClick="checkthis(this)">B
<input type=checkbox name=SceneType value="3" onClick="checkthis(this)">C
<input type=checkbox name=SceneType value="4" onClick="checkthis(this)">D
</form>
</body>
</html>
0
 
LVL 7

Assisted Solution

by:TheKenman
TheKenman earned 50 total points
ID: 10764370
Hi andreni78,

Try this:

<html>
<head>
<script language="javascript">
function doChk(obj){
   var j = 0;
   for(i=0;i<document.yo.elements.length;i++){
      if (document.yo.elements[i].checked) {
         j += +1;
      }
   }
   if (j > 3) {
   alert('Please only select 3 options.');
   obj.checked = false;
   }
}
</script>
</head>        
<body>
<table>                    
<form name="yo" id="yo">
<tr>                              
     <td>&nbsp;</td>
     <td><input type="checkbox" name="SceneType" value="1" onClick="javascript:doChk(this);">A</td>
</tr>
<tr>                              
     <td>&nbsp;</td>
     <td><input type="checkbox" name="SceneType" value="2" onClick="javascript:doChk(this);">B</td>
</tr>
<tr>                              
     <td>&nbsp;</td>
     <td><input type="checkbox" name="SceneType" value="3" onClick="javascript:doChk(this);">C</td>
</tr>
<tr>                              
     <td>&nbsp;</td>
     <td><input type="checkbox" name="SceneType" value="4" onClick="javascript:doChk(this);">D</td>
</tr>
<tr>                              
     <td>&nbsp;</td>
     <td><input type="checkbox" name="SceneType" value="5" onClick="javascript:doChk(this);">E</td>
</tr>
</table>
</form>
</body>
</html>
0
 
LVL 7

Expert Comment

by:TheKenman
ID: 10764374
Dang, someone beat me to it..
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 63

Accepted Solution

by:
Zvonko earned 300 total points
ID: 10764410
Here my version:


<html>
<head>
<script>
function checkBox(theBox){
  allBox=theBox.form[theBox.name];
  chkd = 0;
  for(i=0;i<allBox.length;i++){
    if(allBox[i].checked==true)chkd++;
  }
  if(chkd>3){
    alert("More then three boxes checked.");
    theBox.checked = false;
  }
}
</script>
</head>
<body>
<form>
<table>
<tr>                              
    <td>&nbsp;</td>
    <td><input type="checkbox" name="SceneType" value="1" onClick="checkBox(this)">A</td>
</tr>
<tr>                              
    <td>&nbsp;</td>
    <td><input type="checkbox" name="SceneType" value="2" onClick="checkBox(this)">B</td>
</tr>
<tr>                              
    <td>&nbsp;</td>
    <td><input type="checkbox" name="SceneType" value="3" onClick="checkBox(this)">C</td>
</tr>
<tr>                              
    <td>&nbsp;</td>
    <td><input type="checkbox" name="SceneType" value="4" onClick="checkBox(this)">D</td>
</tr>
<tr>                              
    <td>&nbsp;</td>
    <td><input type="checkbox" name="SceneType" value="5" onClick="checkBox(this)">E</td>
</tr>
</table>
</form>
</body>
</html>


0
 
LVL 15

Assisted Solution

by:VincentPuglia
VincentPuglia earned 50 total points
ID: 10764769
And yet another version:

<html>
<head>
<script type='text/javascript'>
var chkd = 0;
function checkit(chObj)
{
  chkd++;
  if (chkd > 3)
  {
    alert("More then three boxes checked.");
  chObj.checked = false;
  }
}
</script>
</head>
<body>
<form>
<table>
<tr>                              
    <td>&nbsp;</td>
    <td><input type="checkbox" name="SceneType" value="1" onClick="if (this.checked) checkit(this)" >A</td>
</tr>
<tr>                              
    <td>&nbsp;</td>
    <td><input type="checkbox" name="SceneType" value="2" onClick="if (this.checked) checkit(this)">B</td>
</tr>
<tr>                              
    <td>&nbsp;</td>
    <td><input type="checkbox" name="SceneType" value="3" onClick="if (this.checked) checkit(this)">C</td>
</tr>
<tr>                              
    <td>&nbsp;</td>
    <td><input type="checkbox" name="SceneType" value="4" onClick="if (this.checked) checkit(this)">D</td>
</tr>
<tr>                              
    <td>&nbsp;</td>
    <td><input type="checkbox" name="SceneType" value="5" onClick="if (this.checked) checkit(this)">E</td>
</tr>
</table>
</form>
</body>
</html>

Vinny
0
 
LVL 33

Expert Comment

by:sajuks
ID: 10764892
:-) :-) :-).
' ' ' ' ' ' ' ' ' ' '  ' '
' ' ' ' ' ' ' ' ' ' '  '
Its raining solutions
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10764935
Its raining confusions :-)

Vinny, select four checkboxes, unselect all again and check again one.

0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 10765015
Thanks Zvonko,

That will teach to me to code before I finish my first mug of espresso :D


<script type='text/javascript'>
var chkd = 0;
function checkit(chObj)
{
 
  chkd += (chObj.checked) ? 1 : -1;
  if (chkd > 3)
  {
    alert("More then three boxes checked.");
  chObj.checked = false;
    chkd--;
  }
}
</script>
</head>
<body>
<form>
<table>
<tr>                              
    <td>&nbsp;</td>
    <td><input type="checkbox" name="SceneType" value="1" onClick="checkit(this)" >A</td>
</tr>

Vinny
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10765112
Ok, but now you can have only ONE group of checkboxes to check for max.
My example can check many groups by checkbox name.

0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 10765158
Hi Zvonko,

 I simply threw in the solution to throw it into the ring as another alternative.  I personally wouldn't code it that way for myself -- it uses a global variable (chkd); something I try to stay away.

 Besides I'm still sipping my espresso :D

Vinny
0
 

Author Comment

by:andreni78
ID: 10772734
Thank you all for your help. I choose Zvonko solution because it is elegant, simple, flexible and generic.

Andre
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10772828
Thanks for points and thank you for the feedback.
Splitting points is always the best approach when several experts helped you to find a solution.

Cheers,
Zvonko
0
 
LVL 7

Expert Comment

by:TheKenman
ID: 10772856
Thanks for following up andreni78, and thanks for giving consideration (points) to the rest of us!

Take care,
Kenman
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question