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 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
I'm using monorail, activerecord, and jquery. I have a form with a zip code
I have a form that looks like this : [ enterdata: [______] ; more
I have a sign-up form that has nested associations/attributes whatever you want to call
There is a textbox and a button that validates the entry inside the textbox.
I have done a validation check in my form to check for email,username and
Let's say I have a form_tag in a view gathering a view user input
at the moment i'm using this validation plugin: ( http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/ ) but when it
Update : Gutted entire question with more thorough description Ok same question with different

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.