views:

6710

answers:

7

I thought people would be working on little code projects together, but I don't see them, so here's an easy one:

Code that validates a valid US Zip Code. I know there are ZIP code databases out there, but there are still uses, like web pages, quick validation, and also the fact that zip codes keep getting issued, so you might want to use weak validation.

I wrote a little bit about zip codes in a side project on my wiki/blog:

https://benc.fogbugz.com/default.asp?W24

There is also a new, weird type of zip code.

https://benc.fogbugz.com/default.asp?W42

I can do the javascript code, but it would be interesting to see how many languages we can get here.

+1  A: 

Only 5% of the world uses zip codes. If you are writing a web site, don't forget the other 95% and fail the validation because the user typed in something that fails a US zip code validator.

Mike Thompson
Most of the sites I work on were US-only. I'm not saying this is a global adress validator, but you have to develop each postal system's validator code separately. For example, you couldn't easy create a single piece of code that did the US and Canada.
benc
If you need the address, I would assume you're going to ship something to the address; then how can you be sure you will never, ever need to send something outside your country?
Stein G. Strindhaug
+2  A: 

Javascript Regex Literal:

US Zip Codes: /(^\d{5}$)|(^\d{5}-\d{4}$)/

var isValidZip = /(^\d{5}$)|(^\d{5}-\d{4}$)/.test("90210");

Some countries use Postal Codes, which would fail this pattern.

keparo
You can replace your test regex with /(^\d{5}(-\d{4})?$/ to be a bit more concise. It will also run marginally faster in many regex engines.
Dave Sherohman
@Dave, there's a typo in your optimization, missing the last `)`.
cballou
A: 

Hi, Are you referring to address validation? Like the previous answer by Mike, you need to cater for the othe 95%.

What you can do is when the user select's their country, then enable validation. Address validation and zipcode validation are 2 different things. Validating the ZIP is just making sure its integer. Address validation is validating the actual address for accuracy, preferably for mailing.

Saif Khan
A: 

Here's a JavaScript function which validates a ZIP/postal code based on a country code. It allows somewhat liberal formatting. You could add cases for other countries as well. Note that the default case allows empty postal codes since not all countries use them.

function isValidPostalCode(postalCode, countryCode) {
    switch (countryCode) {
        case "US":
            postalCodeRegex = /^([0-9]{5})(?:[-\s]*([0-9]{4}))?$/;
            break;
        case "CA":
            postalCodeRegex = /^([A-Z][0-9][A-Z])\s*([0-9][A-Z][0-9])$/;
            break;
        default:
            postalCodeRegex = /^(?:[A-Z0-9]+([- ]?[A-Z0-9]+)*)?$/;
    }
    return postalCodeRegex.test(postalCode);
}

FYI The second link referring to vanity ZIP codes appears to have been an April Fool's joke.

Mike Henry
I'm pretty sure that you are referring to the NPR story from 2004 (which I just found). The info in the 2nd link was from 2007, and I think one of the articles I read was actually from the USPS site (I didn't save every link).
benc
A: 

To allow a user to enter a Canadian Postal code with lower case letters as well, the regex would need to look like this:

/^([a-zA-Z][0-9][a-zA-Z])\s*([0-9][a-zA-Z][0-9])$/

Are lower case letters allowed? I took a quick look in the wikipedia entry, and the examples appear to be all upper-case, but it was not stated explicitly.
benc
+3  A: 
function isValidUSZip(sZip) {
   return /^\d{5}(-\d{4})?$/.test(sZip);
}
Andrey Fedorov
A: 

Don't bother "validating" ZIP codes of 5 digits, they're obsolete as explained at http://semaphorecorp.com/cgi/zip5.html

Correct validating of addresses and ZIP+4 codes is done with CASS software against USPS databases.

joe snyder