I would like to use this simple script with my form to make sure I have at least 1 box checked but I have a feeling something is wrong, most likely at the getElementsByName tag. I always get the pop up box no matter if I have items checked or not.
<script language="javascript">
function validate() {
var chks = document.getElementsByName('id[][]');
var hasChecked = false;
for (var i = 0; i < chks.length; i++) {
if (chks[i].checked) {
hasChecked = true;
break;
}
}
if (hasChecked == false) {
alert("Please select at least one.");
return false;
}
return true;
}
</script>
and the form, which may or may not end up with more checkboxes in the end:
<form
enctype="multipart/form-data"
method="post"
action="formsubmission.php"
name="form_type" onSubmit="return validate()">
<input id="attrib-8-10" type="checkbox" value="10" name="id[8][10]">
<label class="Checkbox" for="attrib-8-10">thick</label>
<input id="attrib-8-11" type="checkbox" value="11" name="id[8][11]">
<label class="Checkbox" for="attrib-8-11">medium</label>
<input id="attrib-8-12" type="checkbox" value="12" name="id[8][12]">
<label class="Checkbox" for="attrib-8-12">thin</label>
document.getElementsByName("id")
and change all the names of the inputs to simply "id", then it seems to work. Why the problems were originally caused? I'm not sure, all I know is that it works. hopefully someone else will post an answer explaining why. – rcplusplus Apr 3 '12 at 4:02name
attributes specified this way? Most server-side languages don't need you to specify the index, just the fact that you want the form elements treated as an array (in which case you would useid[]
) – inspector-g Apr 3 '12 at 4:15