8

I have a form that validates zip codes on a min/max length basis. I need to have the zip min be 5 digits for all countries EXCEPT Australia which needs to be 4. Here is what Im having trouble with:

$.validator.addMethod(
    "AusZip",
    function(value, element) {
        if ($("#Country").val("Aus") && ("#PostalCode").length < 4)) {
        return false;
    } else return true;
},
"Australian Zip Code Must Be at least 4 Digits");

then in the rules

rules: {
    PostalCode: {
        required: true,
        minlength: 5 //for all countries except AUS
        AusZip: true // for Aus
    }
}

Is length not the way to go?

1 Answer 1

8

I'm assuming all validation rules must pass, which means that your minlength will always fail if you have a length of 4.

Also, you're missing a $ before ("#PostalCode").length.

Also this line sets the value of #Country.

$("#Country").val("Aus")

You want to get the value, and compare it to "Aus".

$("#Country").val() === "Aus"

Try removing minlength, and changing your custom function.

Try this:

EDIT: Changed so that you have 2 validators.

One verifies that the county is Australia and the length of the value is at least 4.

The other verifies that the county is not Australia and the length of the value is at least 5.

$.validator.addMethod("AusZip", function(value, element) {
    var isAus = $("#Country").val() === "Aus";

    if ( isAus && value.length < 4 ) {
        return false;
    } else return true;

}, "Australian Zip Code Must Be at least 4 Digits");

$.validator.addMethod("NonAusZip", function(value, element) {
    var isNotAus = $("#Country").val() !== "Aus";

    if ( isNotAus && value.length < 5 ) {
        return false;
    } else return true;

}, "Zip Code Must Be at least 5 Digits");


$('form').validate({
    rules: {
        PostalCode: {
            required: true,
            AusZip: true,
            NonAusZip: true
        }
    }
});​

Or if you don't need a custom validation message based on the country, you could do this:

$.validator.addMethod("GlobalZip", function(value, element) {
    var isAus = $("#Country").val() === "Aus";

    if ( ( isAus && value.length < 4 ) || value.length < 5 ) {
        return false;
    } else return true;

}, "Zip Code is not long enough");

$('form').validate({
    rules: {
        PostalCode: {
            required: true,
            GlobalZip: true
        }
    }
});​
Sign up to request clarification or add additional context in comments.

7 Comments

so if I remove "minlength" from the PostalCode rules will it still require a minlength of 5 for all others?
this returns an error "errorFirstWord" is not defined for some reason in console.
It should. Give it a try. This line checks for Is Australia and less than four, OR is less than five. (isAus && $pCode.length < 4) || $pCode.length < 5
@Dirty - Sorry about that. Stupid mistake on my part. Just get rid of errorFirstWord + in the error string since it is not accessible outside the function.
I don't see why there is an error for "errorFirstWord" its defined locally in the function
|

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.