473,671 Members | 2,255 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 1846
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
4737
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
6613
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
2426
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
27775
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
2340
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
4141
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
2249
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
5176
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
2062
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
8476
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
8393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8914
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8820
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
8670
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...
0
7433
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4224
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...
1
2810
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
2
1809
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.