473,511 Members | 16,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to require a user to check 2 boxes and no more than 2 checkboxes with different n

14 New Member
Hello,

I want to have a user to check at least 2 check boxes and NO more than 2 boxes. I have different checkbox names because I stored each nam on different field.
I tried this code but nothing happened.

Here is my code:
function clickCtr(chk){
var ctr = 0;
var frm = chk.form;
for (var i=1; i<=4; i++){
if (frm.elements["q1"+i].checked) ctr++;
}
if (ctr==2){
for (i=1; i<=4; i++){ //disable remaining unchecked checkboxes
if (!frm.elements["q1"+i].checked) frm.elements["q1"+i].disabled=true;
}
}
else{ //enable all
for (i=1; i<=4; i++){
frm.elements["q1"+i].disabled=false;
}
}
}

function chFrm() {
...
var ctr = 0;
var chks = document.frmName.elements["q1s"];
for (var i=1; i<=4; i++){
if (document.frmName.elements["q1"+i].checked) ctr++;
}
if (ctr<2){
msgbox = msgbox + "\n Please check 2 boxes.";
}
...
}

Here is my form:

<input type="checkbox" name="q11" id="q2" value="b" />b<BR />
<input type="checkbox" name="q12" id="q3" value="c" />c<BR />
<input type="checkbox" name="q13" id="q4" value="d" />d<BR />
<input type="checkbox" name="q111" id="q2" value="b" />b<BR />
<input type="checkbox" name="q112" id="q3" value="c" />c<BR />
<input type="checkbox" name="q113" id="q4" value="d" />d<BR />

Thanks for your help.
Jun 11 '07 #1
10 1831
epots9
1,351 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. function chFrm() 
  2. {
  3.     var ctr = 0;
  4.     var chks = document.frmName.q;
  5.     var msgbox;
  6.     for (var i=0; i<chks.length; i++)
  7.     {
  8.         if (chks[i].checked)
  9.         {
  10.             ctr++;
  11.         }
  12.     }
  13.     if (ctr<2 || ctr>2)
  14.     {
  15.         msgbox = msgbox + "\n Please check 2 boxes.";
  16.     }
  17.     else if(ctr == 2)
  18.     {
  19.         for (var i=0; i<chks.length; i++)
  20.         {
  21.             chks[i].disabled = !chks[i].checked
  22.         }
  23.     }
  24. }
  25.  
[HTML]
<form name="frmName">
<input type="checkbox" name="q" id="q2" value="b" />b<BR />
<input type="checkbox" name="q" id="q3" value="c" />c<BR />
<input type="checkbox" name="q" id="q4" value="d" />d<BR />
<input type="checkbox" name="q" id="q2" value="b" />b<BR />
<input type="checkbox" name="q" id="q3" value="c" />c<BR />
<input type="checkbox" name="q" id="q4" value="d" />d<BR />
<input type="button" name="submit" value="submit" onclick="chFrm();" />
</form>
[/HTML]

what i posted above will will disable the checkboxs when only 2 are selected, when the button it clicked. if u dont' want the button use this:

Expand|Select|Wrap|Line Numbers
  1.         <script type="text/javascript">
  2. function chFrm() 
  3. {
  4.     var ctr = 0;
  5.     var chks = document.frmName.q;
  6.     var msgbox;
  7.     for (var i=0; i<chks.length; i++)
  8.     {
  9.         if (chks[i].checked)
  10.         {
  11.             ctr++;
  12.         }
  13.     }
  14.     if (ctr<2 || ctr>2)
  15.     {
  16.         msgbox = msgbox + "\n Please check 2 boxes.";
  17.         for (var i=0; i<chks.length; i++)
  18.         {
  19.             chks[i].disabled = false
  20.         }
  21.     }
  22.     else if(ctr == 2)
  23.     {
  24.         for (var i=0; i<chks.length; i++)
  25.         {
  26.             chks[i].disabled = !chks[i].checked
  27.         }
  28.     }
  29. }
  30.  
[HTML]
<form name="frmName">
<input type="checkbox" name="q" id="q2" value="b" onclick="chFrm();" />b<BR />
<input type="checkbox" name="q" id="q3" value="c" onclick="chFrm();" />c<BR />
<input type="checkbox" name="q" id="q4" value="d" onclick="chFrm();" />d<BR />
<input type="checkbox" name="q" id="q2" value="b" onclick="chFrm();" />b<BR />
<input type="checkbox" name="q" id="q3" value="c" onclick="chFrm();" />c<BR />
<input type="checkbox" name="q" id="q4" value="d" onclick="chFrm();" />d<BR />
</form>
[/HTML]

u'll have to play with it to make it work with your code.

tell me how it goes, good luck.
Jun 11 '07 #2
tadisaus2
14 New Member
The reason I have different checkbox names is to store in different field of each name.
Here is my checkbox names:
<input type="checkbox" name="q11" id="q2" value="b" />b<BR />
<input type="checkbox" name="q12" id="q3" value="c" />c<BR />
<input type="checkbox" name="q13" id="q4" value="d" />d<BR />
<input type="checkbox" name="q111" id="q2" value="b" />b<BR />
<input type="checkbox" name="q112" id="q3" value="c" />c<BR />
<input type="checkbox" name="q113" id="q4" value="d" />d<BR />

I like to have a js validation to check for ... if user check ONLY one box, when submitting the form generate error. If a user check 2 boxes, the rest of the boxes become disabled.

Thanks.
Jun 11 '07 #3
epots9
1,351 Recognized Expert Top Contributor
then u have to do it manually, no forloop. u have to make a if statement for each checkbox
Jun 11 '07 #4
tadisaus2
14 New Member
Can I use this?
if (!frm.elements["q1"+i].checked) frm.elements["q1"+i].disabled=true;

So it adds 1 to all checkboxes names.

thanks.
Jun 11 '07 #5
epots9
1,351 Recognized Expert Top Contributor
Can I use this?
if (!frm.elements["q1"+i].checked) frm.elements["q1"+i].disabled=true;

So it adds 1 to all checkboxes names.

thanks.
nope, cuz your names go q11, q112, q12, etc.

and your for loop will go q10, q11, q12, q13, q14, q15
Jun 11 '07 #6
acoder
16,027 Recognized Expert Moderator MVP
You could use the form.elements array and check the type if these are the only checkboxes in the form.
Jun 12 '07 #7
tadisaus2
14 New Member
This is a good idea.
How do I use form.elements?
thanks.
Jun 12 '07 #8
acoder
16,027 Recognized Expert Moderator MVP
See this link
Jun 12 '07 #9
tadisaus2
14 New Member
Yes. I tried that but still does not work.
Here is the code:
function chFrm() {
...
var ctr = 0;
var el, i=1;
while (typeof (el=document.frmName.elements["color"+(i++)]) != 'undefined') {
if (el.checked) ctr++;
}
if (ctr<2){
msgbox = msgbox + "\n Please check 2 boxes.";
}
...
}

thanks.
Jun 12 '07 #10
acoder
16,027 Recognized Expert Moderator MVP
If these are the only checkboxes, you could try:
Expand|Select|Wrap|Line Numbers
  1. for (i=0; i<frm.elements.length; i++) {
  2.   if (frm.elements[i].type == "checkbox")
  3.     if (frm.elements[i].checked) num++;
  4. }
Jun 13 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
4730
by: LRW | last post by:
I have no idea if this is more a PHP question or Javascript question, because my problem hinges equally on both. I have a PHP script that queries a database and creates a list of rows for each...
7
6593
by: Mike Lopez | last post by:
Hello. I need to set a checkbox to the "checked" state and prevent the user from unchecking it. I tried using "disabled", but then the value is not passed on the Post to an ASP page. Anyone...
2
2422
by: Edward | last post by:
The following html / javascript code produces a simple form with check boxes. There is also a checkbox that 'checks all' form checkboxes hotmail style: <html> <head> <title></title> </head>...
2
27716
by: Travis.Box | last post by:
I have an MS Access userform with 16 Check Boxes. Each of the checkboxes has a different option value, which coincides with the Check Box name (eg. cb01.OptionValue = 1). At the bottom of the...
5
2336
by: Stephen | last post by:
Could someone please help me with some validation. I have to write code which checks to see whether a dropdown list is populated with a value or a checkbox is checked. I want the code to run on the...
1
4131
by: babu17 | last post by:
hi, i have a list of checkboxes, the number of checkboxes is dynamic. All the checkboxes have same name. i am trying to get the length of selected checkboxes. <input type=checkbox name=name1...
2
2244
by: birwin | last post by:
I found a Javascript snippet that very effectively checks all boxes on a page, even on a page on which the input tags are in tables and on which I use a lot of other javascript. Unfortunately it is...
5
5163
by: vovan | last post by:
I have set of controls (Textboxes, checkboxes etc) along with the Grid on Windows Form. I use BindingSource to populate both Grid and the set of Controls. User selects the record in the grid and...
11
2045
by: Patrick | last post by:
Trying this question again in a different way and expanding it to another newsgroup. Looking for how I would do this. For an html form; Say I have three check boxes A, B, and C . When I click...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.