Everything developers need to know about UK postcode structure, validation, and working with postcode data in applications.
A UK postcode is a series of letters and numbers that identifies a specific geographic area for mail delivery. Every UK address has a postcode. There are approximately 1.7 million active UK postcodes covering England, Scotland, Wales, and Northern Ireland.
UK postcodes are not the same as ZIP codes used in the USA. They are more granular — a UK postcode typically covers around 15 properties on a single street.
A UK postcode has two parts separated by a space:
| Part | Name | Example | Description |
|---|---|---|---|
NR21 | Outward code | NR21 | Identifies the postal district. 2-4 characters. |
8AB | Inward code | 8AB | Identifies the specific sector and unit. Always 3 characters: digit + 2 letters. |
The outward code itself has two parts:
| Part | Name | Example | Description |
|---|---|---|---|
| Area | Postcode area | NR | 1-2 letters. Identifies the postal area (e.g. NR = Norwich). |
| District | Postcode district | 21 | 1-2 digits (sometimes with a letter). Identifies the district within the area. |
| Postcode | Area | District | Sector | Unit | Location |
|---|---|---|---|---|---|
SW1A 1AA | SW | 1A | 1 | AA | Buckingham Palace, London |
NR21 8AB | NR | 21 | 8 | AB | Fakenham, Norfolk |
EC1A 1BB | EC | 1A | 1 | BB | City of London |
BT1 1AA | BT | 1 | 1 | AA | Belfast, Northern Ireland |
EH1 1AA | EH | 1 | 1 | AA | Edinburgh, Scotland |
CF10 1AA | CF | 10 | 1 | AA | Cardiff, Wales |
There are several valid postcode formats depending on the area code length and district format:
| Format | Example | Pattern |
|---|---|---|
| AN NAA | E1 6AN | Single letter area, single digit district |
| ANN NAA | NR21 8AB | Single letter area, two digit district |
| AAN NAA | SW1A 1AA | Single letter area, digit + letter district |
| AAN NAA | EC1A 1BB | Two letter area, digit + letter district |
| ANN NAA | CF10 1AA | Two letter area, two digit district |
| AN NAA | BT1 1AA | Two letter area, single digit district |
Use this regex to validate UK postcodes in your application. It handles all valid UK postcode formats including special cases:
// JavaScript const UK_POSTCODE_REGEX = /^[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}$/i; // Usage UK_POSTCODE_REGEX.test('NR21 8AB'); // true UK_POSTCODE_REGEX.test('NR218AB'); // true (no space accepted) UK_POSTCODE_REGEX.test('ZZ99 9ZZ'); // false (invalid)
# Python import re UK_POSTCODE_REGEX = re.compile(r'^[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}$', re.IGNORECASE) UK_POSTCODE_REGEX.match('NR21 8AB') # Match UK_POSTCODE_REGEX.match('SW1A 1AA') # Match
# PHP $pattern = '/^[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}$/i'; preg_match($pattern, 'NR21 8AB'); // 1 (match)
Before validating or looking up a postcode, normalise it to a consistent format:
// JavaScript — normalise to uppercase with space function normalisePostcode(raw) { const p = raw.toUpperCase().replace(/\s+/g, '').trim(); return p.slice(0, -3) + ' ' + p.slice(-3); } normalisePostcode('nr218ab'); // 'NR21 8AB' normalisePostcode('SW1A1AA'); // 'SW1A 1AA' normalisePostcode(' ec1a 1bb '); // 'EC1A 1BB'
Once you have a valid, normalised UK postcode, you can look up coordinates, drive times, and street names using the Job Bookers Routing API.
GET https://api.jobbookers.co.uk/v1/geocode?postcode=NR21+8AB { "postcode": "NR21 8AB", "lat": 52.83162, "lon": 0.87049 }
The Job Bookers Routing API converts UK postcodes to coordinates, drive times, distances and street names. Flat monthly pricing from £19/month.
View pricingYour bill is the same every month regardless of how you use your allocation. No overage charges, ever.
More from Job Bookers Routing API