title: How to validate US ZIP code using jQuery Validation plugin
body: <p>I am using the <a href="http://jqueryvalidation.org/" rel="noreferrer">jQuery Validation plugin</a> for validating my form.</p>
  
  <p>I want to validate the zip code as per US ZIPCode format :-</p>
  
  <pre><code>&gt; 12345
  &gt; 12345-6789
  &gt; 12345 6789
  </code></pre>
  
  <p>As per my curent code its validating the zipcode should be maximum 5 and all numeric.</p>
  
  <p>Please see the code below:</p>
  
  <pre><code>    &lt;script src="~/Scripts/js/jquery.validate.js"&gt;&lt;/script&gt;
  
  &lt;script&gt;
  
      $().ready(function () {
  
          // validate signup form on keyup and submit
          $("#locationSetup").validate({
              rules: {
                  'Address.AddressStreet': "required",
                  'Address.City': "required",
                  'Address.State.Country.CountryIsoCode': "required",
                  'Address.State.SubnationalIsoCode': "required",
                  'Address.PostalCode': {
                      required: true,
                      minlength: 5,
                      maxlength: 5,
                      digits: true
                  }
              },
              messages: {
                  'Address.AddressStreet': "Please enter your Street Address!",
                  'Address.City': "Please select your City!",
                  'Address.State.Country.CountryIsoCode': "Please  select your Country!",
                  'Address.State.SubnationalIsoCode': "Please  select your State!",
                  'Address.PostalCode': {
                      required: "Please enter your Postal Code!",
                      minlength: "Your Postal Code must be 5 numbers!",
                      maxlength: "Your Postal Code must be 5 numbers!",
                      digits: "Your Postal Code must be 5 numbers!"
                  }
              }
          });
  
      });
  &lt;/script&gt;
  
  @using (Html.BeginForm(Model.Step.ToString(), "Provision", FormMethod.Post, new Dictionary&lt;string, object&gt; { { "id", "locationSetup" } }))
  
  &lt;table width="100%" id="locInfo"&gt;
              &lt;colgroup&gt;
                  &lt;col width="30%" /&gt;
                  &lt;col /&gt;
              &lt;/colgroup&gt;
              &lt;tr&gt;
                  &lt;th&gt;&lt;label&gt;@AddressResource.COUNTRY_LABEL_TEXT:&lt;/label&gt; &lt;span class="redtext"&gt;(Required)&lt;/span&gt;&lt;/th&gt;
                  &lt;td&gt;
                     @* @Html.HiddenFor(m =&gt; m.Address.State.Country.CountryIsoCode)*@
                     @Html.DropDownListFor(m =&gt; m.Address.State.Country.CountryIsoCode, ProvisioningHubProxy.GetCountriesList(),
                                  htmlAttributes: new { @id = "Countries", @name = "Countries" })
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;th&gt; &lt;label&gt;@AddressResource.STATES_LABEL_TEXT:&lt;/label&gt;&lt;span class="redtext"&gt;(Required)&lt;/span&gt;&lt;/th&gt;
                  &lt;td&gt; @Html.DropDownListFor(m =&gt; m.Address.State.SubnationalIsoCode, ProvisioningHubProxy.GetStatesList(),
                                  htmlAttributes: new { @id = "States", @name = "States" })&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;th&gt;&lt;label&gt;@AddressResource.CITY_LABEL_LABEL_TEXT:&lt;/label&gt;&lt;span class="redtext"&gt;(Required)&lt;/span&gt;&lt;/th&gt;
                  &lt;td&gt; @Html.TextBoxFor(m =&gt; m.Address.City, htmlAttributes: new { @name = "City", @id = "City" })&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;th&gt; &lt;label&gt;@AddressResource.STREET_NAME_LABEL_TEXT:&lt;/label&gt;&lt;span class="redtext"&gt;(Required)&lt;/span&gt;&lt;/th&gt;
                  &lt;td&gt;@Html.TextBoxFor(m =&gt; m.Address.AddressStreet, htmlAttributes: new { @name = "StreetName", @id = "StreetName" })&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;th&gt;&lt;label&gt;@AddressResource.US_POSTAL_CODE_LABEL_TEXT:&lt;/label&gt;&lt;span class="redtext"&gt;(Required)&lt;/span&gt;&lt;/th&gt;
                  &lt;td&gt;@Html.TextBoxFor(m =&gt; m.Address.PostalCode, htmlAttributes: new { @name = "ZipCode", @id = "ZipCode", @required = "required" })&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
  </code></pre>
  
  <p>}</p>
  
  <p>Please suggest.</p>