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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

count checkboxes

Status
Not open for further replies.

ptichka

Programmer
May 28, 2002
19
US
Hello everyone,

I have 10 checkboxes. But I want the user to only be able to select 3 checkboxes out of 10. How can I check that the user did not select more?


Thanks.
 
increment a counter through a loop of the group and validate while in the loop if the count reaches > 3

need scripting help? or just the logic?

____________________________________________________
Python????? The other programming language you never thought of!
thread333-584700

onpnt2.gif
 
I'm certain 873 people will have a much better idea in about ten minutes, but here would be my approach:

Code:
<html>
<head>
<script>
var checkNum = 10;
var checkOnly = 3;
function checkCount() {
   var checks = 0;
   for (var i = 0; i < checkNum; i++) {
      if (document.getElementById('check_' + i).checked) {
         checks++;
         if (checks > checkOnly) {
            alert('oops');
            return false;
         }
      }
   }
   alert('count is good');
   return true
}
</script>
</head>
<body>
<input type=&quot;check&quot; id=&quot;check_0&quot; onclick=&quot;checkCount()&quot;>
...
<input type=&quot;check&quot; id=&quot;check_9&quot; onclick=&quot;checkCount()&quot;>
</body>
</html>

Hope this helps.
Steve
 
one of the 873 people saying it looks good to me

if this is not a group you can also validate .type = 'checkbox'

this would run through all object of type being a checkbox

____________________________________________________
Python????? The other programming language you never thought of!
thread333-584700

onpnt2.gif
 
Ha! I was going to say that I'm sure there are as many ways to do this as there are people racing to tell you how.

Here's one that is very similar to Steve's above (included only because I went to the trouble) and per the logic of onpnt:

Code:
<HTML>
<HEAD>
<SCRIPT>
var cbCollection;
var LIMIT = 3;
function collectCBs()
{
 cbCollection = document.getElementsByName(&quot;CB&quot;);
}//end collectCBs()

function validate(cbChecked)
{
 var count=0;
 for(i=0; i<cbCollection.length; i++)
 {
  if(cbCollection[i].checked)
   count++;
  if(count > LIMIT)
  {
   alert(&quot;You may only select &quot; + LIMIT + &quot; choices.\nUncheck another selection before choosing this.&quot;);
   cbChecked.checked = false;
   break;
  }//end if
 }//end for
}//end validate()
</SCRIPT>
</HEAD>
<BODY ONLOAD=collectCBs()>
<INPUT TYPE=CHECKBOX NAME=CB VALUE=0 ONCLICK=validate(this)> cb0
<INPUT TYPE=CHECKBOX NAME=CB VALUE=1 ONCLICK=validate(this)> cb1
<INPUT TYPE=CHECKBOX NAME=CB VALUE=2 ONCLICK=validate(this)> cb2
<INPUT TYPE=CHECKBOX NAME=CB VALUE=3 ONCLICK=validate(this)> cb3
<INPUT TYPE=CHECKBOX NAME=CB VALUE=4 ONCLICK=validate(this)> cb4
<INPUT TYPE=CHECKBOX NAME=CB VALUE=5 ONCLICK=validate(this)> cb5
<INPUT TYPE=CHECKBOX NAME=CB VALUE=6 ONCLICK=validate(this)> cb6
<INPUT TYPE=CHECKBOX NAME=CB VALUE=7 ONCLICK=validate(this)> cb7
<INPUT TYPE=CHECKBOX NAME=CB VALUE=8 ONCLICK=validate(this)> cb8
<INPUT TYPE=CHECKBOX NAME=CB VALUE=9 ONCLICK=validate(this)> cb9
</BODY>
</HTML>

 
Thanks so much everybody for helping.
I got exactly what I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top