Postcode Validation

Wikipedia cracks me up sometimes.  I'm sat here at my desk, trying to knock together some C# code to quickly validate an entered string as being a UK postcode.  Naturally I turned to this Wikipedia article for assistance, where I find this quip:

"Automatic validation of postcodes on the basis of pattern feasibility is therefore almost impossible to design, and the system contains no self-validating feature such as a check digit. Completely accurate validation is only possible by attempting to deliver mail to the address, and verifying with the recipient."

I think that's what is known as a long-running transaction!!

Update - the C# code that I wrote to handle this issue is available here.

#1  avatar Jez on 26 May 2007 at 05:00

Do the Post Office have a web API for postcode validation? I reckon anyone who's been coding long enough has had at least one stab at it, and for correct verification I guess the postcode is just stripped of whitespace, hashed and compared against a (huge) list of known postcodes. Easy, yeah? US zip codes are possibly a simpler scheme; any 5-digit number will do. Very easy to validate!

 

#2 ian avatar Ian Nelson on 27 May 2007 at 10:39

The Royal Mail make a vast amount of money from licensing their copyrighted Postcode Address File, as do third parties such as QAS who licence this data to develop software solutions.  So no, there's no handy Web API.

All I really want is some code to *quickly* parse a string as (probably) being a valid postcode, and breaking it down into the "outer" and "inner" postcode.  I think I can achieve this using some regular expressions rather than making a costly call to a database or web service (costly in terms of performance, and licensing fees!)

 

#3  avatar John Topley on 28 May 2007 at 05:00

I think it depends what you mean by valid postcode. You could have a postcode that's theoretically valid but that doesn't actually correspond to a real address. I wrote some VB6 code years ago at work to check the former and we had the PAF stored in SQL Server for checking the latter.

#4 ian avatar Ian Nelson on 28 May 2007 at 06:08

John, you're right, I should have been more specific in my original post.  I'm knocking together some code to check that a string conforms to the definition of a postcode given in BS7666, so theoretically valid as you say.

#5  avatar Nathan on 25 Jan 2008 at 13:02

This regular expression matches all valid postcodes, and filters out most obvious syntax errors:

/^([A-Z][A-Z]?[0-9][0-9A-Z]? *[0-9][A-Z][A-Z])$/

However, filtering out invalid postcodes isn't possible without having a list of all valid postcodes. "BH1 1QP" is invalid even though it's syntactically correct.

#6  avatar Ryan on 13 Jun 2008 at 12:07

The wiki comment made me laugh!

Automatic validation of postcodes on the basis of pattern feasibility is therefore almost impossible to design, and the system contains no self-validating feature such as a check digit. Completely accurate validation is only possible by attempting to deliver mail to the address, and verifying with the recipient.

I am writting a .NET app that uses the QAS webservice, am not sure sending the mail to the address for validation would be sufficient in my system, not even sure how Id code that hahaha!

#7  avatar Robert on 13 Aug 2008 at 13:00

See : www.braemoor.co.uk/.../postcodes.shtml

Free scripts for validating post codes

#8 ian avatar Ian Nelson on 13 Aug 2008 at 13:05

Thanks Robert, you have some nice validation scripts there.

#9  avatar Dave R. on 01 Sep 2008 at 16:08

A more comprehensive regex is:

/^(GIR 0AA|BFPO ([0-9]{1,4})|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2})$/

This covers BS7666 plus BFPO addresses. Unfortunately Nathan's regex will report many invalid postcodes as valid.

Valid formats (where A=alpha, 9=digit):

A9 9AA, A99 9AA, A9A 9AA, AA9 9AA, AA99 9AA, AA9A 9AA, BFPO 9, BFPO 99, BFPO 999, BFPO 9999, GIR 0AA

I recently took great satisfaction in replacing several hundred lines of crappy Javascript code with a one-liner based on this ;)

The only way to ensure that your app is 100% accurate is to subscribe to the daily PAF update from The Royal Mail. However, I believe that most of us would be happy with a quarterly feed.

#10  avatar Paul on 25 Oct 2009 at 01:29

Not exactly a coding comment but I thought someone here might know something about postcodes. I'm in Australia and we send all our tax receipts to a Postcode numbered 1936. Nowhere on the Australia Post website or on their postcode database is there a '1936' postcode. When I try to use their application that retrieves a price quote for delivery of a certain size package from a postcode to '1936' it states "this is not a valid postcode". Strange? - since the withdrawal always seems to come out of my bank account when I send mail t this address. Can anyone think of what may be going on if on the exterior there appears to be no such postcode? Does the postal service operate two different databases - they obviously seem able to process mail to a postcode which they publicly say is invalid? Does anyone know what a "non-official postcode" is?

#11  avatar Tria on 25 Feb 2010 at 12:18

Ah, postcode validity... minefield. Most sites won't accept the postcode of my new flat as valid, even though it is, which is a LOT of fun, I can tell you...

Leave a Comment