...

View Full Version : checking 1 of 3 checkboxes?



dentaldude
10-10-2003, 12:05 AM
First off, great forum - I have found many answers here so far, but something eludes me....

I have a form with 3 checkboxes per question because I want to allow a user to select more than one response, however I want to make sure they check atleast 1 of each of the 3 responses. For instance:

<form name="formcheck">
<input type="checkbox" name="C1" value="ON">
<input type="checkbox" name="C2" value="ON">
<input type="checkbox" name="C3" value="ON">

<input type="checkbox" name="C4" value="ON">
<input type="checkbox" name="C5" value="ON">
<input type="checkbox" name="C6" value="ON">

<input type="checkbox" name="C7" value="ON">
<input type="checkbox" name="C8" value="ON">
<input type="checkbox" name="C9" value="ON">
</form>

So they should check atleast 1 out of C1, C2, C3, one of C4, C5, C6, etc,etc. There is a total of 45 checkboxes (up to C45).

if anyone can help me with a form validation code that can do this i would appreciate it

thanks
dentaldude

Basscyst
10-10-2003, 06:52 AM
You can try this I didn't test it but it might work:

Code:
function validateForm()
{
count1=3;
count2=0

for(i=0;i<count1;i++)
{
wuz_checked=document.formcheck.element[i].checked;

if(wuz_checked==true)
{
count1=count1+3;
count2=count2+3;
i=count2;
}
else
{
alert("Please Check At Least One Box Per Category");
}
if(i>formcheck.length){
return;
}
}
}

scriptkeeper
10-10-2003, 07:25 AM
This works!



<html>
<head>
<script>
function isChecked(eles){
var e=eles.split(',');
var isChk=false;
for(var i=0;i<e.length;i++){
if(document.getElementById(e[i]).checked){
isChk=true;
break;
}
}
if(!isChk) alert("you must make atleast one selection");

return isChk;
}
</script>
</head>
<body>
<form name="formcheck1" onsubmit="isChecked('C1,C2,C3');">
<input type="checkbox" name="C1" value="ON">
<input type="checkbox" name="C2" value="ON">
<input type="checkbox" name="C3" value="ON">
<br><input type="submit" value="Check Group 1">
</form>

<form name="formcheck2" onsubmit="isChecked('C4,C5,C6');">
<input type="checkbox" name="C4" value="ON">
<input type="checkbox" name="C5" value="ON">
<input type="checkbox" name="C6" value="ON">
<br><input type="submit" value="Check Group 2">
</form>

<form name="formcheck3" onsubmit="isChecked('C7,C8,C9');">
<input type="checkbox" name="C7" value="ON">
<input type="checkbox" name="C8" value="ON">
<input type="checkbox" name="C9" value="ON">
<br><input type="submit" value="Check Group 3">
</form>


</form>
</body>
</html>

dentaldude
10-10-2003, 01:11 PM
That does work and thanks, but I don't want seperate submit buttons for each question, it would be easier to have 1 submit button for the whole form, follow?

is there a way to modify this script to account for that?

thanks in advance

Roelf
10-10-2003, 02:18 PM
i dont have time to make something better, but this works. I relies on a grouping attribute in the checkbox tags


<html>
<head>
<script>
function checkit (objForm) {
var group;
var previousgroup = 0;
var thisgroupcheck = true;
for (var i = 0; i < objForm.elements.length; i++){
var elem = objForm.elements[i]
if (elem.type == "checkbox") {
group = elem.group;
if (group != previousgroup) {
if (!thisgroupcheck) {
alert ("group " + previousgroup + " has no check");
}
previousgroup = group;
thisgroupcheck = false;
}
if (elem.checked || thisgroupcheck) {
thisgroupcheck = true;
}
}
}
// verify the last group
if (!thisgroupcheck) {
alert ("group " + previousgroup + " has no check");
}
}
</script>
</head>


<body>
<form name="formcheck">
<input type="checkbox" name="C1" value="ON" group="1">
<input type="checkbox" name="C2" value="ON" group="1">
<input type="checkbox" name="C3" value="ON" group="1">
<br>
<input type="checkbox" name="C4" value="ON" group="2">
<input type="checkbox" name="C5" value="ON" group="2">
<input type="checkbox" name="C6" value="ON" group="2">
<br>
<input type="checkbox" name="C7" value="ON" group="3">
<input type="checkbox" name="C8" value="ON" group="3">
<input type="checkbox" name="C9" value="ON" group="3">
<br>
<input type="button" onclick="checkit(this.form);">
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum