JavaScript Form Validation: At Least One Checkbox/Radio Button

Technical Information Type: 

I needed to validate that at least one checkbox or radio button was checked (selected) in an HTML form, without knowing whether there would be more than one. If I assumed that there would be an array, but only one checkbox or radio button existed, then it would not work. No array can be assumed, because the actual number of options will be dependent on data coming from a database, rather than a hard-coded form with a set number of options.

The Fix: 

In the following, replace myForm with the actual form and myCheckbox with the name of the form control being checked. If checking a radio button instead of a checkbox, change the alert (although a more custom alert is probably more sensible anyway, such as "select at least one style of beer").

var checkFound = false;
for (var counter=0; counter < myForm.length; counter++) {
   if ((myForm.elements[counter].name == "myCheckbox") && (myForm.elements[counter].checked == true)) {
      checkFound = true;
if (checkFound != true) {
   alert ("Please check at least one checkbox.");

Fix License: 
Creative Commons: Attribution-ShareAlike