...

View Full Version : CheckBox Validation



tomyknoker
12-10-2007, 11:43 PM
Anyone know how I can make sure that out of a group of 10 checkboxes atleast 1 is checked?

tomyknoker
12-11-2007, 12:21 AM
I did try this but it doesn't seem to work as an or statement both of them have to be checked for the JavaScript to pass:


if (!document.form.check111.checked || !document.form.check112.checked) {
theMessage = theMessage + "\n --> Q10 Please select atleast 1 topic";
}

Philip M
12-11-2007, 07:42 AM
If you used the search feature you would see that this has been covered many times before.

Example:-


<form name="myform" method="POST" action="" onsubmit="return checkTheBox();">
<input type="checkbox" name="check[]" value="1" /> 1 &nbsp;&nbsp;
<input type="checkbox" name="check[]" value="2" /> 2 &nbsp;&nbsp;
<input type="checkbox" name="check[]" value="3" /> 3 &nbsp;&nbsp;
<input type="checkbox" name="check[]" value="4" /> 4 &nbsp;&nbsp;
<input type="checkbox" name="check[]" value="5" /> 5 &nbsp;&nbsp;
<input type="submit" value="Submit Form" />
</form>

<script type = "text/javascript">
function checkTheBox() {
var flag = 0;
for (var i = 0; i< 5; i++) {
if(document.myform["check[]"][i].checked){
flag ++;
}
}
if (flag == 0) {
alert ("You must check at least one checkbox!");
return false;
}
return true;
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum