我怎样才能检查我的输入一堆新线,我知道这不是很实用,但我不能100%我的输入不是易碎。我怎样才能检查在javascript输入框的空格使用“输入”
因此,我该如何检查javascript中的输入框是否只是一堆空格,而我现在只需检查空格。
谢谢
我怎样才能检查我的输入一堆新线,我知道这不是很实用,但我不能100%我的输入不是易碎。我怎样才能检查在javascript输入框的空格使用“输入”
因此,我该如何检查javascript中的输入框是否只是一堆空格,而我现在只需检查空格。
谢谢
您可以使用正则表达式:
if (str.replace(/[\s\n\r]+/g, "") != "")
{
// if you remove spaces and line breaks, it doesn't equal nothing
}
编辑 - 它应该是更快
if (/\S/.test(str))
{
// found something other than spaces or line breaks
// "\S" should match any character that is not a space or line break
}
空间只是一堆空格。所以,你可以用下面的方法来检查:
if(s.indexOf(' ') >= 0) return true;
您可能还需要修整的,而不是检查空格的潜力存在你输入:
s.trim();
您可以使用修剪和检查的长度。
// it is a bunch of spaces
if(inputValue.trim().length == 0){ /* ... */}
感谢猪头,是缺少的东西雷 – Jim 2014-10-20 16:55:39