views:

38

answers:

1

I know how to display an error message if the user leaves a required input field blank when filling in a form, but I am not able to do the same with the textarea.

+1  A: 

i think on change fires for text areas as well so assuming it does

theTextArea.onChange = function(){
  if(this.value == ''){
     alert('You must supply a value for this field');
  }
};

or with jquery

$(document).ready(
  function(){
    $('#mytextarea').change(function(){
      var $this = $(this);
      if($this.val() == ''){
         alert('You must supply a value for this '+$("label[for='mytextarea']").text());
      }
    });
});
prodigitalson
I would change this.value == '' to /^\s*$/.test(this.value) so it matches whenever there's only whitespace.
Matthew Flaschen