2

Users of my application are asked for their country and postal code. I want to use the postal codes as a key in data store. Because the postal codes are entered by the user they can have been entered in different forms. For example a Canadian user might have entered:

  • A1A 1A1
  • a1a 1a1
  • A1A1A1
  • a1A 1A1
  • etc...

Now I can make a normalization function for Canadian postal codes that chooses one canonical form (for example, all uppercase without spaces) and convert the postal codes to the canonical form before saving it as a DB key.

But, that only covers Canada. I don't want to reinvent the wheel. Is there a library or API out there that can normalize (and possibly validate) postal codes for all (or many) countries?

Thanks!

Adam
  • 43,763
  • 16
  • 104
  • 144
  • Why not make it easier? Rather than process all of variety codes entered instead one can ask users to input in standard form only (it is normal practise in fact) and check what has been put by one simple format. It easier to implement a support for standard code representation of all countries and you can do it manually, I believe, without big tricky library. Users are alright to input correct when they are asked/explained how it shall be. – Ruslan Gerasimov Jun 26 '14 at 00:00
  • @RuslanGerasimov, ok, that's a valid point, in that case is there a place where I can get a list of standard formats for all countries? – Adam Jun 26 '14 at 00:03
  • also check this thread they pretend to cover Canadian and other world's codes with regex and other stuff [http://stackoverflow.com/questions/578406/what-is-the-ultimate-postal-code-and-zip-regex](http://stackoverflow.com/questions/578406/what-is-the-ultimate-postal-code-and-zip-regex) – Ruslan Gerasimov Jun 26 '14 at 00:05
  • [ISO 3166-1](http://en.wikipedia.org/wiki/ISO_3166-1) , [ISO 3166-1 alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) and others. There is an organization for this called Universal Postal Union [http://www.upu.int](http://www.upu.int) – Ruslan Gerasimov Jun 26 '14 at 00:10
  • @RuslanGerasimov: Easier for who? Clearly you are not a Canadian, having to do all that damn shift-unshift typing. Adam: Do not use the codes as keys. Eg Britain & the US have already modified their zone systems. – philipxy Jun 26 '14 at 06:46
  • Easier for developer. No I am not Canadian. When users are told what is exact format expected at input, is it then easier for them as well rather than guess again what it could be from a bloody variety? – Ruslan Gerasimov Jun 26 '14 at 06:51
  • 1
    I'm going to build a library for this in Node – Anthony Aug 29 '16 at 16:36

2 Answers2

2

It's pretty easy to do with Regular Expressions and string functions built into your language.

The CountryInfo table from Geonames contains Postal Code Regex and Format values for all countries with postal codes:

http://download.geonames.org/export/dump/countryInfo.txt

Don't forget that H0H 0H0 is a valid postal code for Santa Claus.

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
2

The important thing to remember with Postal Codes is that not all countries have them, and as addresses get added, updated or removed, Post Codes can get re-coded.

With that in mind, if you want to ensure the data you're capturing is accurate, the best solution is to usually use a paid for third party library or web service to validate this. There are a number of companies that provide services to validate the entire address, and not just the post code. These will vary in global coverage and cost, depending on your needs.

I work for a company which provides address validation for most countries world wide through a consistent interface, either a web service or API you can integrate with.

You can try out some examples here: http://www.qas.co.uk/knowledge-centre/product-information/address-postcode-finder.htm

Akshay
  • 3,361
  • 1
  • 21
  • 19