繁体   English   中英

如何检查输入文本字段是否只包含空格?

[英]How to check whether the input text field contains only white spaces?

在Javascript中检查输入文本字段是否为空(仅包含空格或仅包含空格)的最简单方法是什么?

var str = document.getElementById("myInput").value;
if (str.match(/^\s*$/)) {
    // nothing, or nothing but whitespace
} else {
    // something
}

你正在寻找像修剪功能的东西,对吧?

在某处包含此功能(为了提供修剪功能)

String.prototype.trim = function () {
   return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

请参阅http://javascript.crockford.com/remedial.html

然后...

if (document.forms['id_of_form'].elements['id_of_input'].value.trim()=='') {
    //do xyz
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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