I am assuming I need a regular expression here?
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:
My input is basic enough:
<label for="dzip"><span class="red">♥ </span>Zip:</label>
<input name="attributes[address_zip]" id="dzip" type="text" size="30" class="required zip-code" />
And then my script for city was easy enough. I just need to apply it to zip-code as well:
jQuery.validator.addMethod("Chicago", function(value) {
return value == "Chicago";
}, '**Recipient must reside in Chicago City Limits**');
May help if I show how the plug-in functions for phone-numbers (US). Basically I need to translate this to zips:
jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
phone_number = phone_number.replace(/\s+/g, "");
return this.optional(element) || phone_number.length > 9 && phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");
What does this part do/mean?:
phone_number.replace(/\s+/g, "")
I dropped in the phone part directly from an example on the validate site.
Final answer from all the great (and quick) input here was this:
jQuery.validator.addMethod("zip-code", function(zip_code, element) {
zip_code = zip_code.replace(/\s+/g, "");
return this.optional(element) || zip_code.length == 5 && zip_code.match(^606[0-9]{2}$);
}, "Please specify a City of Chicago Zip Code");
for the length use the .length property
for the 606 use the substr method
combined
Try this:
The above code can be read as