473,787 Members | 2,931 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.frmNam e.elements["q1s"];
for (var i=1; i<=4; i++){
if (document.frmNa me.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 1857
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.fr mName.elements["color"+(i+ +)]) != 'undefined') {
if (el.checked) ctr++;
}
if (ctr<2){
msgbox = msgbox + "\n Please check 2 boxes.";
}
...
}

thanks.
Jun 12 '07 #10

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

Similar topics

6
4749
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 item, each with their own checkbox one can select to do stuff later. The PHP script this form posts to creates an array of all the selected checkboxes and does stuff. But, on the form, I want to be able to have the checkboxes auto-check when...
7
6620
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 have any ideas? Thanks in advance,
2
2437
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> <body> <form name="myform" action=test.php method=post>
2
27817
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 form is a command button. When the command button is clicked, I need the VBA scripting to determine which Check Box has been selected, and then assign a vaule to a string based on which Check Box has been selected. What I'm having trouble figuring...
5
2344
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 on-click of a button. My page has 1 dropdown list and two checkboxes. So I either want the user to choose an item from the dropdownlist OR tick a checkbox(it doesn't matter if they tick both checkboxes). I can't allow the user to fill in the...
1
4148
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 value=1> <input type=checkbox name=name1 value=2> ........ get dynamically. when i try to get the length in javascript like
2
2251
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 too effective and checks all boxes in all groups of checkboxes - even disabled checkboxes. I need a work-around (or new code) that will limit it to only check one group of boxes, perhaps by using a unique id for the input tag. (I tried adding &&...
5
5182
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 all controls are populated with data from the selected row. The grid is going to be read only. Textboxes, checkboxes are going to be read/write. What event and how do I need to use to catch any change in any of the textboxes? I tried to use...
11
2072
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 on a checkbox a dropdown window will open next to it and the selections will be 1, 2, and 3. I want to use the same script and dropdown for each of the 3 check boxes and be able to return values independent of each other. Meaning if I check box A...
0
9655
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
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 we have to send another system
3
2894
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.