Author
John Angeles
Updated
2022-04-14
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 | FastSimple to understandUnlikely to change over time | Does not check if postcode valid | Ideal for sanity checks |
Complex Regular Expression Check if a postcode looks correct and only permits patterns known to be correct | FastDoes exclude some invalid postcodes | False positives & negativesRequires continual testingDifficult to understand & debug | 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 | FreeAlso lists terminated postcodes | External service (HTTP/DB call)Dataset up to 3 months old | 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 | Updated Daily | ExpensiveExternal service (HTTP/DB call) | Ideal for postcode validation cost permitting |
Simple Regular Expression
A simple postcode regular expression, or postcode regex, checks the general shape of the postcode is correct. i.e.
- Is the string between 5 to 7 characters?
- Is the inward code first character numeric?
- Are the last 2 characters non-numeric?
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
const { isValid } = require("postcode");
isValid("Sw1a 2aA")
Advantages
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.
- Fast. Can be completed in nanoseconds
- Simple. Can be easily understood
- Highly unlikely to change over time. Uses well known patterns
Disadvantages
- Does not verify whether postcode exists. Only whether it conforms to the correct schema
Use Case
The primary use case for this approach is as a sanity check. Instances include to:
- Check whether a postcode before dispatching a HTTP database or network request
- Provide quick feedback in UI, if postcode needs to be corrected
Complex Regular Expression
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})
Advantages
The main advantage of this approach over a simple regex is it narrows the set of valid postcodes.
Disadvantages
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).
- False Positives and Negatives still possible. Complex regular expressions still allow invalid postcodes and in some instances can reject valid postcodes
- Requires Continual Testing. A simple search will yield different postcode regular expressions. However, many of these are old and not systematically tested. Even government provided regular expressions change over time and have been incorrect for certain valid postcodes. To implement this approach with confidence, a system of continual testing against the latest list of postcodes should be used
- Complex. Some time is required to understand how the regular expression works. In some instances a poorly formed regular expression can result in a denial of service vulnerability known as ReDoS
Use Case
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.
Office for National Statistics (ONS) Postcode Directory
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:
const PostcodesIO = require("postcodesio-client") const postcodes = new PostcodesIO();
await postcodes.lookup("SW1A2AA");
Advantages
- Free. ONSPD is generally free to use (with some exception for Northern Ireland)
- Terminated Postcodes. The list of deactivated postcodes is useful to rule out a scenario where a postcode has been marked as invalid because it has been put out of service
Disadvantages
- External Service Required. The dataset is large, which means postcode validation likely requires an external API or database request
- Dataset can be up to 3 months old. ONSPD is updated every quarter, which makes it unsuitable for some use cases.
Use Case
ONSPD can be reliably used as a postcode validation service so long as data currency is not a prohibitive factor.
Royal Mail Postcode Address File
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:
const { Client } = require("@ideal-postcodes/core-node"); const client = new Client({ api_key: "ak_test" });
await client.lookupPostcode({ postcode: "SW1A2AA" });
Advantages
- Updated Daily. The primary advantage of PAF over ONSPD is data currency. PAF is updated daily
- Premise Data Available. Unlike ONSPD, PAF can also return a complete list of addresses at a given postcode
Disadvantages
- Expensive. Each search on the PAF dataset likely yield a licence fee charge
- External Service Required. The dataset is large, which means postcode validation likely requires an external API or database request
Use Case
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.