Hi, I'm new to the forum and not partivularly proficient with Javascript. That said, I am trying to build a simple script which will verify whether atleast one of an array of check boxes, corresponding to vendor categories, is selected. This is for exhibitor application to an arts & crafts festival. Within the same script I also need to check whether a particular checkbox has been selected (the "Other" category), and if so, check that the user has typed something in the adjacent text box, specifying what the unlisted category is. function CheckCategories() { boxes = frm_VendorAp.category.length for (i = 0; i < boxes; i++) { if (frm_VendorAp.category[i].checked == false) { alert("Please select a Vendor Category"); return false; } else if (frm_VendorAp.category[11].checked == true && frm_VendorAp.other_specified.value == "") { alert("Please specify your medium or product type"); frm_VendorAp.other_specified.focus(); return false; } else {return true;} } } Code (markup): Below is the code for a couple of checkboxes: <input name="category" type="checkbox" value="Prints" tabindex="18"> <input name="category" type="checkbox" value="Other" tabindex="22"> Code (markup): So far the script invalidates input if any of the checkboxes are unselected, though it does prompt for input if the second condition (the "Other" category) is fulfilled. The corresponding checkbox is no. 11 in the array. What do I need to change for the script to accept a single checkbox selection? Thanks
Untested, but give this a try. <script type="text/javascript"> <!-- function CheckCategories() { var boxes = frm_VendorAp.category; var check = false; for (var i in boxes) { if (frm_VendorAp.category[i].checked) { check = true; break; } } if (frm_VendorAp.category[11].checked && frm_VendorAp.other_specified.value == "") { alert("Please specify your medium or product type"); frm_VendorAp.other_specified.focus(); return false; } else if (!check) { alert("Please select a Vendor Category"); return false; } else { return true; } } //--> </script> Code (markup):
I spoke too soon! The script works fine in Firefox, my primary browser, but in IE it displays the alert message no matter how many checkboxes are selected. In other words, it does not seem to recognize the checked property of the checked boxes at all.
Try placing document. infront of all frm_VendorAps. var boxes = document.frm_VendorAp.category; .... if (document.frm_VendorAp.category[i].checked) .... etc... Code (javascript):