Skip to navigation
Real World Computing

Address formats

Posted on 18 Jun 2009 at 11:48

Simon Jones tackles the tricky subject of handling addresses in applications, both within the UK and far beyond.

If you don't have access to the full PAF, there are other ways to validate a postcode. This regular expression can be used to check that a postcode has the right letters and numbers, in the right order, so that at least it is a permissible postcode:

(GIR 0AA|[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})

Comparing a possible postcode against this regular expression will weed out obvious problems such as not having the right number of characters, or using letters that aren't allowed at a particular position - for instance, the letters C, I, K, M, O and V aren't allowed as either of the final two characters. Notice how the regular expression starts with one of the exceptions, by allowing "GIR 0AA", the postcode for the old Post Office Girobank that's still used today by the Alliance & Leicester Commercial Bank, which took over the business in 1989. Checking a postcode against a regular expression is easy in VB.NET or any other .NET language, since there's a RegEx class built in that provides an IsMatch function. You just have to pass it the string you want to test and the regular expression string you want to match against:

If System.Text.RegularExpressions.Regex.IsMatch(strFullPostcode, strRegExPostcodeValidator) Then

...

End If

If you want to go further, lists of all the valid region codes - the one or two letters at the start of each postcode - are available on the internet; a few sites encourage individuals to plot their own postcodes on a map, which makes the resulting data available for free. One of the best is New Popular Edition Maps (www.npemap.org.uk), which uses old Ordnance Survey maps from the 1930s and 1940s that are now out of copyright - these have been scanned and stitched together to give a patchwork map of England, Wales and Scotland, although maps of Northern Ireland don't come out of copyright for a few years yet. There are about 1.7 million unique postcodes in the UK (covering the 27 million addresses at an average of 15 addresses per postcode) and NPE Maps has so far collected 41,718 of these. Even though this represents only 2.5% of all the postcodes in the UK, the site has 98% of the outward codes (the first half of a postcode) and 93% of the sectors (the outward half plus the first digit of the inward half). The biggest problems facing the site's database are cleaning mistakes - where people entered the wrong postcode or clicked the wrong place on the map - and getting that last few per cent of the data.

If you want the official list of postcode sectors you can buy the Post Town Gazetteer from the Royal Mail for just £50, which lists the outward code and inward sector for all postcodes, plus the corresponding post town and the old county. Using this information, you can reduce the data-entry burden for a new address to just typing the postcode and house number. You can buy similar data for many countries of the world from www.geopostcodes.com, which includes latitude and longitude data.

CEDEX headache

The biggest problem I've had when dealing with European addresses is the French CEDEX system. Courrier d'Entreprise ? Distribution EXceptionnelle (CEDEX) is designed for recipients of large volumes of mail, in which each organisation is allocated its own unique postal code; you have to use that code followed by the word CEDEX to have the letter delivered. La Post, the French postal service, would like you to write the postal code, city, the word CEDEX and possibly a sector or Arrondissement number all on one line, in that order, so for example:

1 2 3 4
Subscribe to PC Pro magazine. We'll give you 3 issues for £1 plus a free gift - click here

From around the web

User comments

NZ postcodes

They don't require an island in a NZ address. Otherwise, an informative article.

By OwenWatson on 29 Aug 2009

Leave a comment

You need to Login or Register to comment.

(optional)

Simon Jones

Simon Jones

Simon is a contributing editor to PC Pro. He's an independent IT consultant specialising in Microsoft Office, Visual Basic and SQL Server.

Read more More by Simon Jones

advertisement

Latest Real World Computing
Latest Blog Posts Subscribe to our RSS Feeds
Latest News Stories Subscribe to our RSS Feeds
Latest ReviewsSubscribe to our RSS Feeds

advertisement

Sponsored Links
 
SEARCH
SIGN UP

Your email:

Your password:

remember me

advertisement


Hitwise Top 10 Website 2010
 
 

PCPro-Computing in the Real World Printed from www.pcpro.co.uk

Register to receive our regular email newsletter at http://www.pcpro.co.uk/registration.

The newsletter contains links to our latest PC news, product reviews, features and how-to guides, plus special offers and competitions.