Skip to content

Instantly share code, notes, and snippets.

@preaction
Created December 28, 2009 14:30
Show Gist options
  • Select an option

  • Save preaction/264697 to your computer and use it in GitHub Desktop.

Select an option

Save preaction/264697 to your computer and use it in GitHub Desktop.
/** oneCheckboxIsCheckedInForm( form, message, matchName )
* Returns true if at least one checkbox in the form is checked
* Will show an alert message otherwise
* Use in an onsubmit handler thus:
* return oneCheckboxIsCheckedInForm(this)
*/
function oneCheckboxIsCheckedInForm( form, message, matchName ) {
if ( !message ) {
message = "You must check at least one checkbox before printing";
}
if ( !matchName ) {
matchName = ".";
}
// loop through all the elements in the form
for (var i = 0; i < form.elements.length; i++) {
// If the element is a checkbox
if (form.elements[i].type == "checkbox" && form.elements[i].name.match( matchName ) ) {
// If the checkbox is checked
if (form.elements[i].checked) {
return true;
}
}
}
// else
alert(message);
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment