OverviewZIP code is an important part of a mailing address. If you plan to use a mailing address, you should store the ZIP code. Without at ZIP code, your postal mail will not be delivered as quickly as possible.
(If you want to be very modern (a.k.a. efficient), you should build all your address handling around ZIP codes. For example, if you have an interface for looking up an address in a call center, ask for ZIP code first. Nobody seems to do this, but you should.)
Style GuideThe proper use of the term is "ZIP code" or "ZIP Code" (not "Zip")
ZIP -> 5 digits
ZIP + 4 ->10 characters (5 digits + "-" + 4 digits)
- ZIP + 4 (vanity ZIP code)
Additional formats: US Postal bar codes can add another two digits, and (in theory) create a 12-digit, point address.
Alternative formats: The field that stores ZIP code might be over-loaded in usage, and support other postal codes (like "Canadian Postal Code"). Some multi-country sites simply use the label "Postal Code" rather than "ZIP code." Other sites use auto-changing fields, but this can introduce a lot of complexity.
- The first three digits (the original coding system) point to a "sectional center facility"
"The next two digits represent the sectional center facility (e.g. 432xx = Columbus OH), and the fourth and fifth digits represents the area of the city (if in a metropolitan area), or a village/town (outside metro areas): 43209 (4=Ohio,32=Columbus,09=Bexley). When a sectional center facility's area crosses state lines, that facility is assigned separate three-digit prefixes for the states that it serves; thus, it is possible to identify the state associated with any ZIP Code just by looking at the first three digits."
- ZIP codes are actually delivery paths for mail, not locations:
"Returned government parcels from the District of Columbia are sent to ZIP codes beginning with "569", so that returned parcels are security checked at a remote facility (this was put into place after the anthrax scare)."
- taking them too literally, as a location...
"Despite the geographic derivation of most ZIP codes, the codes themselves do not represent geographic regions; they generally correspond to address groups or delivery routes. Consequently, ZIP Code "areas" can overlap, be subsets of each other, or be artificial constructs with no geographic area. Similarly, in areas without regular postal routes (rural route areas) or no mail delivery (undeveloped areas), ZIP Codes are not assigned or are based on sparse delivery routes, and hence the boundary between ZIP code areas is undefined."
Use the correct text label ("ZIP"). On multi-national sites, "Postal Code", if you do not localize the field label.
Store as a 10 digit string (or 5 digit if you do not support ZIP+4). Do not store as a numeric value.
Validate the format as "a string with 5 digits". ZIP+4 is "a string with 5 digits, a hyphen, and 4 digits".
Test Case: ZIP + 4 acceptance test - "-0001"
"It is common to use add-on code 9998 for mail addressed to the postmaster (to which requests for pictorial cancellations are usually addressed), 9999 for general delivery and other high-numbered add-on codes for business reply mail. For a unique ZIP code (explained below), the add-on code is typically 0001."
Test Case: ZIP+4 Acceptance test - "12345-6789"
"The ZIP+4 code of 12345-6789 belongs to the General Electric Company facility located in Schenectady, NY. That facility used to be the company headquarters. The property is in both Schenectady and the Town of Rotterdam, NY"
Test Case: "do you support Santa Claus?" - "North Pole 12345"
"ZIP code 12345 belongs to the world headquarters for General Electric in Schenectady, New York. Each year, the facility receives many letters from children to Santa Claus, addressed to "North Pole 12345"."
Test Case: are you storing the value as numeric? "00501".
"The lowest ZIP code is in Holtsville, New York (00501, a ZIP Code exclusively for the U.S. Internal Revenue Service center there). Other low ZIP codes are 00601 for Adjuntas, Puerto Rico; 01001 for Agawam, Massachusetts, and 01002 for Amherst, Massachusetts. Up until 2001 there were also six zip codes even lower than 00501 that were numbered from 00210 to 00215 (located in Portsmouth, New Hampshire) and were used by the Diversity Immigrant Visa program to receive applications from non-US citizens."
Edge Case: "1234-" (correct length, characters, wrong format).
In terms of business environment changes, a classic bug problem is that of using a numerical field (instead of a character field) for zip codes. The bug appears when zip codes that begin with '0' suddenly start getting truncated because the leading zero is meaningless in a numerical field. An interesting complication of this problem would be a deployed system for a company that has done business in nothing but California for several years and so the problem goes unnoticed. One day, they get a new client in Massachusetts and the system can't print invoices correctly for that new client. You might wonder why the system is "suddenly" failing, but looking at the change in clientele would give you the hint you need.
The "idea" approach, IMHO: define a blocks of valid character types (digit, hyphen), and then scan down the string in blocks around the structure of the format. This is easy to read, is open to many nice error messages, and leverages the relationship of ZIP and ZIP+4.
- Flexibility over many little functions. If there is one function that can handle all formats of a data type, then re-factoring the page is easy, especially if you have a test harness.