Skip to content Skip to sidebar Skip to footer

How To Check The Textarea Content Is Blank Using Javascript?

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

Solution 1:

Try this:

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

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

Solution 2:

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

Example:

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

--

<buttononclick="isEmpty()">Is Empty?</button><formname="myForm"><inputtype="text"name="myField" /></form>

Solution 3:

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

Solution 4:

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

varvalue; // we'll assume it's definedif(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?

Post a Comment for "How To Check The Textarea Content Is Blank Using Javascript?"