views:

472

answers:

2

How do i check if a textarea contains nothing?

I tryed with this code

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

But it doesnt 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?

+4  A: 

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,"");
}
Marcos Placona
You might want to trim the value, too.
Alsciende
Cool idea @Alsciende, added a version with it as well
Marcos Placona
Thanks this worked for me
streetparade
A: 

There is a world of difference between null and empty !

Try this instead:

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