'IsNullOrWhitespace'在 JavaScript 中?

发布于 2024-10-30 06:11:45 字数 289 浏览 1 评论 0原文

是否有相当于 .NET 的 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

不及他 2024-11-06 06:11:45

对于简洁的现代跨浏览器实现,只需执行以下操作:

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();
}

For a succinct modern cross-browser implementation, just do:

function isNullOrWhitespace( input ) {
  return !input || !input.trim();
}

Here's the jsFiddle. Notes below.


The currently accepted answer can be simplified to:

function isNullOrWhitespace( input ) {
  return (typeof input === 'undefined' || input == null)
    || input.replace(/\s/g, '').length < 1;
}

And leveraging falsiness, even further to:

function isNullOrWhitespace( input ) {
  return !input || input.replace(/\s/g, '').length < 1;
}

trim() is available in all recent browsers, so we can optionally drop the regex:

function isNullOrWhitespace( input ) {
  return !input || input.trim().length < 1;
}

And add a little more falsiness to the mix, yielding the final (simplified) version:

function isNullOrWhitespace( input ) {
  return !input || !input.trim();
}
醉生梦死 2024-11-06 06:11:45

自己动手非常简单:

function isNullOrWhitespace( input ) {

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

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

It's easy enough to roll your own:

function isNullOrWhitespace( input ) {

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

    return input.replace(/\s/g, '').length < 1;
}
冷默言语 2024-11-06 06:11:45

不,但你可以写一个

function isNullOrWhitespace( str )
{
  // Does the string not contain at least 1 non-whitespace character?
  return !/\S/.test( str );
}

no, but you could write one

function isNullOrWhitespace( str )
{
  // Does the string not contain at least 1 non-whitespace character?
  return !/\S/.test( str );
}
淡忘如思 2024-11-06 06:11:45

尝试一下

检查字符串是否未定义、为 null、不是 typeof string、empty 或 space(s

/**
  * 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');

Try this out

Checks the string if undefined, null, not typeof string, empty or space(s

/**
  * 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;
}

You can use it like this

isStringNullOrWhiteSpace('Your String');
娇女薄笑 2024-11-06 06:11:45

您必须编写自己的:

function isNullOrWhitespace(strToCheck) {
    var whitespaceChars = "\s";
    return (strToCheck === null || whitespaceChars.indexOf(strToCheck) != -1);
}

You must write your own:

function isNullOrWhitespace(strToCheck) {
    var whitespaceChars = "\s";
    return (strToCheck === null || whitespaceChars.indexOf(strToCheck) != -1);
}
诗化ㄋ丶相逢 2024-11-06 06:11:45

trim() 是 JS 缺少的一个有用的字符串函数。

添加它:

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,"") }

然后: if (document.form.field.value.trim() == "")

trim() is a useful string-function that JS is missing..

Add it:

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,"") }

Then: if (document.form.field.value.trim() == "")

爱殇璃 2024-11-06 06:11:45

提取两个最佳答案的相关部分,您会得到如下结果:

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.");

结果为:

  • RegExp.replace 方法 = 33 - 37 毫秒
  • RegExp.test 方法 = 11 - 14 毫秒

Pulling the relevant parts of the two best answers, you get something like this:

function IsNullOrWhitespace(input) {
    if (typeof input === 'undefined' || input == null) return true;
    return !/\S/.test(input); // Does it fail to find a non-whitespace character?
}

The rest of this answer is only for those interested in the performance differences between this answer and Dexter's answer. Both will produce the same results, but this code is slightly faster.

On my computer, using a QUnit test over the following code:

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.");

The results were:

  • RegExp.replace method = 33 - 37 milliseconds
  • RegExp.test method = 11 - 14 milliseconds
記柔刀 2024-11-06 06:11:45

您可以使用正则表达式 /\S/ 来测试字段是否为空格,并将其与空检查结合起来。

例如:

if(textBoxVal === null || textBoxVal.match(/\S/)){
    // field is invalid (empty or spaces)
}

You can use the regex /\S/ to test if a field is whitespace, and combine that with a null check.

Ex:

if(textBoxVal === null || textBoxVal.match(/\S/)){
    // field is invalid (empty or spaces)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文