简体   繁体   English

从javascript函数返回true或false

[英]Returning true or false from javascript function

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". 问题是当验证函数中的控制台输出显示“验证失败”时,上面的if / else转到else子句。 So I must not be calling that function right. 所以我一定不能正确地称这个功能。

What's the correct way to do what I'm trying to do? 做我正在做的事情的正确方法是什么?

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. ......但真正的问题(正如Teemu 指出的那样 )不在你的功能中,而在于它的使用。 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." 你的函数回答了一个问题,“这是一个有效的邮政编码字符串吗?”,但你对它的使用是说:“如果我的函数说的话,说这是无效的。”

Actually your validation function doesn't return true when validation fails. 实际上,验证失败时验证函数不会返回true You just check the value incorrectly, it should be: 你只是错误地检查了它,它应该是:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM