Need help using jQuery validation rules.

Posted on 2011-10-19
Last Modified: 2012-05-12
Need help using jQuery validation rules. I have a input field that needs specific rules. See below. I'm not sure if you can use range with multiple ranges.

Any help would be great thanks.

rules: {
      zip: {required: true, digits: true, maxlength: 9, minlength: 5},

1) Zip should accept either 5 or 9 digits.
2) Should not accept 6 digits or 7 digits or 8 digits.
Question by:yando18
    LVL 81

    Accepted Solution

    check this :

    test page :

    jQuery.validator.addMethod("zip5or9", function(value, element) {
      return this.optional(element) || value.length == 5 || value.length == 9;
    }, "Please specify a correct zip");

    rules: {
          zip: { required: true, zip5or9: true },

    Open in new window

    LVL 17

    Expert Comment

    leakim971's code is perfect just to show the correct message to end user what they need to enter

    <meta http-equiv="Content-Language" content="en-gb">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Kiran Test</title>
    <script src="" type="text/javascript"></script>
      <script src=""></script>
      $.validator.addMethod("zip5or9", function(value, element) { 
        return this.optional(element) || value.length == 5 || value.length == 9 ; 
    }, "Please specify a correct zip");
        rules: {
          zip: {
            maxlength: 9,
            zip5or9: true
          zip: {
            required: "Please enter zip code",
            digits: "Enter digit only",
            maxlength: "9 digit max",
            minlength: "5 digit minimum",
            zip5or9: "Zip should be either 5 Or 9 digit only"
    <form method="POST" id="testForm" >
      <input name="zip" type="text">
      <input type="submit">

    Open in new window


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now