简体   繁体   中英

how to check the textarea content is blank using javascript?

使用value.length != 0 ..不适用于空白情况

Try this:

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

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

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>

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM