background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London's picture

Normally when there is a form input error, Drupal highlights the form input by adding a red border and changing the text color. Unfortunately, nothing happens for checkboxes and radio buttons. Here's a little jQuery to add the error class to the label directly above a set of checkboxes or radios when an error occurs.

<?php
$('form#node-form div.form-item').each(function(){
  var
result = 0;
 
result += $(this).find('div.form-radios input.form-radio.error').length;
 
result += $(this).find('div.form-checkboxes input.form-checkbox.error').length;
  if (
result) $(this).find('label:first').removeClass('error').addClass('error');
});
?>

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;
  });
});
?>

Syndicate content