4

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?

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... Commented Jan 31, 2013 at 17:59
  • You do understand that "111111111111111" will be considered valid, right? As will ", , , , , ,,, 111111111111111,,,,,,," Commented Jan 31, 2013 at 18:12

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Returning false from regex.exec(listOfZipCodes) === null would give the same result.
Thanks Grinn. That worked. Personally I liked your "or simply" code before you edited it (more readable).
Actually, @Teemu is right (and should be marked as such.) You're just using your function wrong when you call it. I'll update my answer to note this, but please do mark his as the answer - since he mentioned it first.
5

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

1 Comment

Yes. I saw that too, as I was testing Grinn's suggestion. Thanks.
2

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

2 Comments

I'd put \s* before and after the , to allow for white-space padding. But +1 since the regex clearly needs to be fixed,.
Thanks, Christophe & the system. I'm horrible at understanding regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.