www
www

Reputation: 4115

how to check the textarea content is blank using javascript?

using value.length != 0 ..doesn't work for the blank space situation

Upvotes: 2

Views: 11302

Answers (4)

Joel Etherton
Joel Etherton

Reputation: 37533

Try jquery and use its trim() feature. If someone is inputting spaces, value will be neither null nor length == 0.

Upvotes: 1

K Prime
K Prime

Reputation: 5849

Try this:

if (value.match (/\S/)) { ... }

It will make sure value has at least 1 non-whitespace character

Upvotes: 3

Sampson
Sampson

Reputation: 268344

document.myForm.myField.value != ""; // or
document.myForm.myField.value.length == 0;

Example:

function isEmpty() {
  alert(document.myForm.myField.value == "");
}

--

<button onclick="isEmpty()">Is Empty?</button>
<form name="myForm">
  <input type="text" name="myField" />
</form>

Upvotes: 2

Matchu
Matchu

Reputation: 85794

Since you clearly already know how to get the value, I'll skip that bit.

var value; // we'll assume it's defined
if(value) {
    // textarea content is not empty
} else {
    // textarea content is empty
}

'' evaluates to false. Seems simple enough. What blank space situation are you talking about?

Upvotes: 0

Related Questions