Javascript 从javascript函数返回true或false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14632125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:26:17  来源:igfitidea点击:

Returning true or false from javascript function

javascriptreturn-value

提问by marky

I'm doing a regex check on a string within a function:

我正在对函数中的字符串进行正则表达式检查:

function ValidateZipCodeString(listOfZipCodes) {
    var regex = /^([, ]*\d{5})+[, ]*$/,
    matches = regex.exec(listOfZipCodes);

    if (regex.exec(listOfZipCodes) === null) {
        console.log('validation failed');
        return false;
    } else {
        console.log('validation passed');
        return true;
    }
}

The regex is correctly detecting a valid/invalid list of zip codes.

正则表达式正确地检测到有效/无效的邮政编码列表。

I'm calling the function with this:

我用这个调用函数:

console.log('zip code: ' + listOfZipCodes);
if (ValidateZipCodeString(listOfZipCodes)) {
    $tr.find('label#lblCoverageEditError').text('There is invalid text in the list of zip codes. Only 5-digit zip codes allowed.').show();
} else {
    console.log('validate function returned true');
}

The problem is that the above if/else goes to the else clause, when the console output within the validation function shows "validation failed". So I must not be calling that function right.

问题是当验证函数中的控制台输出显示“验证失败”时,上面的 if/else 转到 else 子句。所以我一定不能正确地调用那个函数。

What's the correct way to do what I'm trying to do?

做我想做的事情的正确方法是什么?

回答by Grinn

Your function could be greatly simplified to:

您的功能可以大大简化为:

function ValidateZipCodeString(listOfZipCodes) {
    var regex = /^([, ]*\d{5})+[, ]*$/;

    if (regex.test(listOfZipCodes)) {
        console.log('validation passed');
        return true;
    } else {
        console.log('validation failed');
        return false;
    }
}

...or:

...或者:

function ValidateZipCodeString(listOfZipCodes) {
    var regex = /^([, ]*\d{5})+[, ]*$/;
    return regex.test(listOfZipCodes);
}

...or even just:

......或者甚至只是:

function ValidateZipCodeString(listOfZipCodes) {
    return /^([, ]*\d{5})+[, ]*$/.test(listOfZipCodes);
}

...but the real issue (as Teemu points out) is not in your function, but in the use of it. Your function answers the question, "Is this a valid zip code string?", but your use of it is saying, "Say this is invalid if my function says it is."

...但真正的问题(正如 Teemu指出的)不在于您的功能,而在于它的使用。您的函数回答了“这是一个有效的邮政编码字符串吗?”的问题,但是您对它的使用是说,“如果我的函数说这是无效的,那就说这是无效的。”

回答by Teemu

Actually your validation function doesn't return truewhen validation fails. You just check the value incorrectly, it should be:

实际上,true当验证失败时,您的验证功能不会返回。您只是错误地检查了该值,它应该是:

if (!ValidateZipCodeString(listOfZipCodes)) {
    $tr.find('label#lblCoverageEditError').text('There is invalid text in the list of zip codes. Only 5-digit zip codes allowed.').show();
} else {
    console.log('validate function returned true');
}

回答by Christophe

Others correctly pointed out that you just had your tests in the wrong order. However, and more importantly, your regex is incorrect, as it will for example return true for "1234567890".

其他人正确地指出,您只是按错误的顺序进行了测试。但是,更重要的是,您的正则表达式不正确,例如,它会为“1234567890”返回 true。

Here is a suggestion:

这是一个建议:

function ValidateZipCodeString(listOfZipCodes) {
    return /^\d{5}(\s*,\s*\d{5})*$/.test(listOfZipCodes);
}