Solved

Limit the number of check boxes selected

Posted on 2004-04-06
13
883 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
Comment Utility
<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
Comment Utility
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
Comment Utility
Dang, someone beat me to it..
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 300 total points
Comment Utility
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
Comment Utility
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
Comment Utility
:-) :-) :-).
' ' ' ' ' ' ' ' ' ' '  ' '
' ' ' ' ' ' ' ' ' ' '  '
Its raining solutions
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Its raining confusions :-)

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

0
 
LVL 15

Expert Comment

by:VincentPuglia
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for following up andreni78, and thanks for giving consideration (points) to the rest of us!

Take care,
Kenman
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now