Originally Posted 2001
Only submit if at least one checkbox is checked
A Question I got:
I would like to show an alert if no check box was checked in a field. The idea is that at least one check box should be checked. I have an onSubmit for the form and a JS function that suppose to check it but it does not work.
HTML Source Code
<form action="/" onsubmit="return checkCheckBoxes(this);">
<p><label><input type="CHECKBOX" name="CHECKBOX_1" value="This..."> This...</label></p>
<p><label><input type="CHECKBOX" name="CHECKBOX_2" value="That..."> That...</label></p>
<p><label><input type="CHECKBOX" name="CHECKBOX_3" value="...and The Other"> ...and The Other</label></p>
<p><input type="SUBMIT" value="Submit!"></p>
</form>
Source Code
<script type="text/javascript" language="JavaScript">
<!--
function checkCheckBoxes(theForm) {
if (
theForm.CHECKBOX_1.checked == false &&
theForm.CHECKBOX_2.checked == false &&
theForm.CHECKBOX_3.checked == false)
{
alert ('You didn\'t choose any of the checkboxes!');
return false;
} else {
return true;
}
}
//-->
</script>