Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3224976
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:17:09+00:00 2026-05-17T16:17:09+00:00

I have a form that validates zip codes on a min/max length basis. I

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:17:10+00:00Added an answer on May 17, 2026 at 4:17 pm

    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
            }
        }
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large multi-directional form that validates almost all fields with Jquery Validator,
I have a rails model that validates uniqueness of 2 form values. If these
I have a form that searches all rows in a single table (TServices) that
I have a form that is sending in sizes of things, and I need
I have a view that validates and saves a form. After the form is
I have code that opens a dialog, validates, and posts a form when the
The basics: I have a contact form that uses php to validate the forms.
Say you have a web form with some fields that you want to validate
I have form that displays several keywords (standard set of choice lists that changes
I have a form that contains a GridView control which is databound to an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.