最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

return value - Returning true or false from javascript function - Stack Overflow

programmeradmin4浏览0评论

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.

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

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.

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

Share Improve this question asked Jan 31, 2013 at 17:56 markymarky 5,06818 gold badges63 silver badges109 bronze badges 2
  • I just tried your script with a test string of '43636, 34643' and it worked? I'm not sure what you are having trouble with... – Steve Peak Commented Jan 31, 2013 at 17:59
  • You do understand that "111111111111111" will be considered valid, right? As will ", , , , , ,,, 111111111111111,,,,,,," – the system Commented Jan 31, 2013 at 18:12
Add a comment  | 

3 Answers 3

Reset to default 7

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

Actually your validation function doesn't return true when validation fails. 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".

Here is a suggestion:

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

评论列表(0)

  1. 暂无评论