开发者

how to check the textarea content is blank using javascript?

using value.length != 0 ..doesn't work for the blank space si开发者_如何学编程tuation


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜