background image

Content tagged with: required

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

Eric's picture

To improve usability, I wanted to add a note to the bottom of my form to show that an asterisk indicates a required form field. In this case, I am modifying the user registration form, but the code can be simplified to work with any form API.

<?php
function MYMODULE_form_alter($form_id, &$form) {
 
// ...code...
 
if ($form_id == 'user_register') {
   
$form['requiredNote'] = array(
     
'#value' => "
        <div class='form-item'>
          <label>
            <span class='form-required'>*</span> Indicates Required Field
          </label>
        </div>"
,
     
'#weight' => 10,
    );
  }
 
// ...code...
}
?>