Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp with jQuery validate and a Zip code restriction based on the 1st three digitss
    text
    copied!<p>I am assuming I need a regular expression here? </p> <p>I have a zip code field and the zip codes must be in the city limits of Chicago. Luckily all the zip codes begin with 606. So I need to poll the input to make sure that the zip code entered is 5 digits and begin with the numbers 606:</p> <p>My input is basic enough:</p> <pre><code>&lt;label for="dzip"&gt;&lt;span class="red"&gt;&amp;hearts;&amp;nbsp;&lt;/span&gt;Zip:&lt;/label&gt; &lt;input name="attributes[address_zip]" id="dzip" type="text" size="30" class="required zip-code" /&gt; </code></pre> <p>And then my script for city was easy enough. I just need to apply it to zip-code as well:</p> <pre><code> jQuery.validator.addMethod("Chicago", function(value) { return value == "Chicago"; }, '**Recipient must reside in Chicago City Limits**'); </code></pre> <p>May help if I show how the plug-in functions for phone-numbers (US). Basically I need to translate this to zips:</p> <pre><code> jQuery.validator.addMethod("phoneUS", function(phone_number, element) { phone_number = phone_number.replace(/\s+/g, ""); return this.optional(element) || phone_number.length &gt; 9 &amp;&amp; phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/); }, "Please specify a valid phone number"); </code></pre> <p>What does this part do/mean?:</p> <pre><code>phone_number.replace(/\s+/g, "") </code></pre> <p>I dropped in the phone part directly from an example on the validate site.</p> <p><strong>Final answer from all the great (and quick) input here was this:</strong></p> <pre><code> jQuery.validator.addMethod("zip-code", function(zip_code, element) { zip_code = zip_code.replace(/\s+/g, ""); return this.optional(element) || zip_code.length == 5 &amp;&amp; zip_code.match(^606[0-9]{2}$); }, "Please specify a City of Chicago Zip Code"); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload