是否有JavaScript等同于.NET的String.IsNullOrWhitespace
的方法,以便我可以检查客户端文本框中是否有任何可见文本?
我宁愿先在客户端执行此操作,而不是提交文本框值并仅依赖于服务器端验证,尽管我也会这样做。
是否有JavaScript等同于.NET的String.IsNullOrWhitespace
的方法,以便我可以检查客户端文本框中是否有任何可见文本?
我宁愿先在客户端执行此操作,而不是提交文本框值并仅依赖于服务器端验证,尽管我也会这样做。
要实现一个简洁的现代跨浏览器方案,只需执行以下操作:
function isNullOrWhitespace( input ) {
return !input || !input.trim();
}
这里是jsFiddle。下面有注释。
目前被接受的答案可以简化为:
function isNullOrWhitespace( input ) {
return (typeof input === 'undefined' || input == null)
|| input.replace(/\s/g, '').length < 1;
}
并将虚假性进一步利用:
function isNullOrWhitespace( input ) {
return !input || input.replace(/\s/g, '').length < 1;
}
trim()在所有最新的浏览器中都可用,因此我们可以选择放弃正则表达式:
function isNullOrWhitespace( input ) {
return !input || input.trim().length < 1;
}
再加入一些虚假性质,得到最终(简化)版本:
function isNullOrWhitespace( input ) {
return !input || !input.trim();
}
Uncaught TypeError: input.trim is not a function
。 - JustinStolle自己编写并不难:roll your own
function isNullOrWhitespace( input ) {
if (typeof input === 'undefined' || input == null) return true;
return input.replace(/\s/g, '').length < 1;
}
if (!input) return true;
以使代码更简洁 @Dexter - ccordontrim()
函数? - Kyle Delaney不可以,但你可以写一个
function isNullOrWhitespace( str )
{
// Does the string not contain at least 1 non-whitespace character?
return !/\S/.test( str );
}
typeof(str) == 'string' ? str : ''
,则返回 !/\S/.test(typeof(str) == 'string' ? str : '')
。否则,null、undefined 或任何对象都将返回 true。 - user578895!str || !/\S/.test(str);
。 - NeilHTMLInputElement.value
会返回 null
?只是因为问题提问者认为“null”是可能的值,并不意味着它实际上就是。 - Peter Baileynull
值。http://en.wikipedia.org/wiki/Empty_string - Peter Bailey试试这个
检查字符串是否未定义、为空、不是字符串类型,或者只包含空格
/**
* Checks the string if undefined, null, not typeof string, empty or space(s)
* @param {any} str string to be evaluated
* @returns {boolean} the evaluated result
*/
function isStringNullOrWhiteSpace(str) {
return str === undefined || str === null
|| typeof str !== 'string'
|| str.match(/^ *$/) !== null;
}
isStringNullOrWhiteSpace('Your String');
您可以使用正则表达式/\S/
来测试字段是否为空格,并将其与空值检查相结合。
例如:
if(textBoxVal === null || textBoxVal.match(/\S/)){
// field is invalid (empty or spaces)
}
trim()
是一个 JS 缺失的有用字符串函数.
添加它:
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,"") }
那么:if (document.form.field.value.trim() == "")
你必须编写自己的代码:
function isNullOrWhitespace(strToCheck) {
var whitespaceChars = "\s";
return (strToCheck === null || whitespaceChars.indexOf(strToCheck) != -1);
}
function IsNullOrWhitespace(input) {
if (typeof input === 'undefined' || input == null) return true;
return !/\S/.test(input); // Does it fail to find a non-whitespace character?
}
以下内容仅适用于对此答案与Dexter答案之间性能差异感兴趣的人。两者都会产生相同的结果,但这段代码稍微快一些。
在我的电脑上,使用QUnit测试以下代码:
var count = 100000;
var start = performance.now();
var str = "This is a test string.";
for (var i = 0; i < count; ++i) {
IsNullOrWhitespace(null);
IsNullOrWhitespace(str);
}
var end = performance.now();
var elapsed = end - start;
assert.ok(true, "" + count + " runs of IsNullOrWhitespace() took: " + elapsed + " milliseconds.");
结果如下:
x.value.trim() === ''
(其中x
是指向输入元素的引用)。如果值只包含空白字符,则该表达式将返回 true,否则返回 false。为这样一个简单的任务创建一个专用函数会过度设计。请注意:对于 IE8 及以下版本,您需要实现trim()
函数。这是一项非常简单的任务,而且这也是您想要执行的操作。 - Šime Vidas