How to Validate a UK Postcode. Evaluating the Trade-offs for Various Solutions Including Postcode Lookup Services, Proprietary Postcode Datasets, Free Datasets and Regular Expressions (Regex)
Updated 29 Dec 2020
There are a number of methods you can use to validate UK postcodes. Below are a list of the most commonly used practices.
Method | Advantages | Disadvantages | Use Case |
---|---|---|---|
Simple Regular Expression Checks if a postcode looks correct |
|
|
Ideal for sanity checks |
Complex Regular Expression Check if a postcode looks correct and only permits patterns known to be correct |
|
|
The high complexity and cost of maintenance likely outweighs the benefits |
ONS Postcode Directory Check if a postcode is valid according to the ONS Postcode dataset |
|
|
Ideal for postcode validation if slightly stale (3 month) data is not an issue |
Royal Mail Postcode Address File Check if a postcode is valid according to Royal Mail's dataset |
|
|
Ideal for postcode validation cost permitting |
A simple postcode regular expression, or postcode regex, checks the general shape of the postcode is correct. i.e.
This approach is powered by a simple regular expression pattern match such as:
/^[a-z]{1,2}\d[a-z\d]?\s*\d[a-z]{2}$/i
This is the approach adopted by our open sourced JavaScript library, Postcode.js
The main advantage of this approach is the underlying regular expression is simple to comprehend and unlikely to break as the list of UK postcodes changes.
The primary use case for this approach is as a sanity check. Instances include to:
The complex regular expression approach uses a more restrictive pattern to provide a greater degree of confidence.
An example of such a regular expression is:
([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\s?[0-9][A-Za-z]{2})
The main advantage of this approach over a simple regex is it narrows the set of valid postcodes.
Using this approach makes it difficult to justify adoption as the cost of maintenance likely outweighs the additional benefit (relative to a simple regular expression).
If the resources to understand and continually test this approach are available, then this approach will negate additional patterns not caught by the simple regular expression.
ONS publishes a list of UK postcodes every quarter called the ONS Postcode Directory (ONSPD). This free to use and liberally licensed (with some exception for Northern Ireland).
ONPSD also provides a list of postcodes which are no longer active.
Ideal Postcodes provides an open sourced solution to accessing or hosting postcode validation using ONSPD. Try the JavaScript client below:
ONSPD can be reliably used as a postcode validation service so long as data currency is not a prohibitive factor.
Royal Mail provide a UK address list called the Postcode Address File (PAF) which is updated daily. This can be used to verify whether a postcode is valid without concern for whether the dataset is stale.
PAF is not openly licensed however and a license fee must be extracted to access the dataset.
Ideal Postcodes is a Royal Mail PAF vendor.
An example of how to implement a postcode search on our API:
Ideal for postcode validation if up-to-the-day data currency is truly required. Also provides the ability to retrieve individual premise data for a postcode.