JavaScript中的“IsNullOrWhitespace”? [英] 'IsNullOrWhitespace' in JavaScript?

查看:189
本文介绍了JavaScript中的“IsNullOrWhitespace”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在与.NET相当的JavaScript String.IsNullOrWhitespace 以便我可以检查客户端的文本框中是否有任何可见文本?

Is there a JavaScript equivalent to .NET's String.IsNullOrWhitespace so that I can check if a textbox on the client-side has any visible text in it?

我宁愿在客户端做这个,而不是回发文本框值,只依靠服务器端验证,即使我也会这样做。

I'd rather do this on the client-side first than post back the textbox value and rely only on server-side validation, even though I will do that as well.

推荐答案

这很容易 roll你自己的

function isNullOrWhitespace( input ) {

    if (typeof input === 'undefined' || input == null) return true;

    return input.replace(/\s/g, '').length < 1;
}

这篇关于JavaScript中的“IsNullOrWhitespace”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆