Validating US zip codes

Validating zip codes at the client side can be useful on web pages with address forms.

Entering numbers is an error-prone process. It would be great for the user if we could provide some kind of basic immediate validation to inform them of a possible error in their data entry.

On the other hand, a satisfyingly complete zip code database has a non-trivial size. Loading the complete database at the client side might be difficult and non-optimal.

In this recipe, we're going to write a client-side zip code validation function. In the process, we're going to learn what it takes to convert a non-trivial zip code database to a smaller representation which can be loaded at the client side.

Getting ready

Let's download the zip code database ...

Get HTML5 Data and Services Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.