Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
157
rated 0 times [  161] [ 4]  / answers: 1 / hits: 92488  / 14 Years ago, fri, march 19, 2010, 12:00:00

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?


More From » jquery

 Answers
8

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,);
}

[#97290] Tuesday, March 16, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
johannatorim

Total Points: 599
Total Questions: 124
Total Answers: 100

Location: Virgin Islands (U.S.)
Member since Fri, May 7, 2021
3 Years ago
;