jquery validation for zip code and city
Is there any validation where we can check if the zip code matches the city entered in a form? I came 开发者_开发技巧across several validation to see if it's a valid zip code but nothing of this sort...any idea? the jquery validate plugin just check the empty fields..
There are plenty of zip code databases available but you might run into problems if you're too strict. City names are often very different in these databases than what people will enter. A zip code can map to several cities or several zip codes to one city.
It's probably better to pre fill based on zip but allow the user to type their own city name.
The simplest thing would be to use regex to validate a ZIP code:
function validateZipCode(elementValue){
var zipCodePattern = /^\d{5}$|^\d{5}-\d{4}$/;
return zipCodePattern.test(elementValue);
}
You would need a service to match your zipcode to the city. Something along the lines of
http://zip4.usps.com/zip4/citytown_zip.jsp
or http://zipatlas.com/ (you can download the entire database..)
To avoid matching issues, you could simply omit the city field, and use a service to get the proper city name from the zip code.
You can use cross domain communication with JSON. Check out this link jQuery snippet for more info.
精彩评论