Live warning for required fields

Jan 17, 2009

Here's a little snippet of jquery code that I wrote during our discussion about client-side form validation at DrupalCamp Germany. It simply adds an "error" class (typically a red border around textfields) when you move away from a required field while leaving it empty. While it's functional, it's probably not the best way to write it, I'm just posting it here for informational purpose.

$('.form-required').parents('.form-item').find('input').blur(function() {
  if ($(this).val() == '') {
$(this).addClass('error');
  } 
  else {
$(this).removeClass('error');
  };
});