background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount

Validating checkboxes in a form using jQuery

Eric.London's picture

I created a form using the Drupal forms API that contained a set of checkboxes. Here is the jQuery I added to validate the checkboxes and ensure at least one is checked before submitting the form.

<?php
$(document).ready(function(){
  $(
'form#MYFORMID').submit(function(){
   
isChecked = false;
    $(
'input[@type="checkbox"]', this).each(function(){
      if ($(
this).attr('checked')) isChecked = true;
    });
    if (
isChecked) return true;
    return
false;
  });
});
?>