Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a Textarea is empty in Javascript or jQuery?

Tags:

How do I check if a textarea contains nothing?

I tried with this code:

if(document.getElementById("field").value ==null) {     alert("debug");     document.getElementById("field").style.display ="none";  } 

But it doesn't do what I expect. I expect that it should appear a messagebox "debug" and that the textarea is not shown.

How can I fix that issue?

like image 869
streetparade Avatar asked Mar 19 '10 10:03

streetparade


1 Answers

You wanna check if the value is == "", not NULL.

if(document.getElementById("field").value == '') {     alert("debug");     document.getElementById("field").style.display ="none"; } 

UPDATE

A working example

And another one using TRIM in case you wanna make sure they don't post spaces

Implementation for TRIM()

String.prototype.trim = function() {   return this.replace(/^\s+|\s+$/g,""); } 
like image 51
Marcos Placona Avatar answered Oct 05 '22 20:10

Marcos Placona