how to check the textarea content is blank using javascript?

11,113

Solution 1

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

Solution 2

Try this:

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

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

Solution 3

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>
Share:
11,113
www
Author by

www

I am a pragmatic programmer.

Updated on June 04, 2022

Comments

  • www
    www about 2 years

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