how to check the textarea content is blank using javascript?

折月煮酒 提交于 2019-12-24 00:26:00

问题


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


回答1:


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




回答2:


Try this:

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

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




回答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>



回答4:


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?



来源:https://stackoverflow.com/questions/2090869/how-to-check-the-textarea-content-is-blank-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!