views:

447

answers:

2

How to check for null,white spaces and new lines while validating for a textarea using jquery. If the textarea is empty and has only new lines or spaces should raise an alert

+4  A: 
if($.trim($('#myTextArea').val()) == '') {
    //error
}
Keith Rousseau
see http://api.jquery.com/jQuery.trim/
karim79
New line validation doesnt hold good with this code.I had tried this before
Hulk
From the API Docs: The $.trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string
Keith Rousseau
Strange...............
Hulk
A: 

To get the content of a textarea use $("#textareaid").val()

jQuerys helper function $.trim() will cut all whitespaces before and after the content.

Can can always validate a string on your own, for instance, like

for(var i=0;i < yourstring.length; i++){
   if(yourstring.charCodeAt(i) == 13){
      // do something with return character
   }
}

Kind Regards

--Andy

jAndy