Developer Guide

UK Postcode Format

Everything developers need to know about UK postcode structure, validation, and working with postcode data in applications.

What is a UK Postcode?

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.

UK Postcode Structure

A UK postcode has two parts separated by a space:

PartNameExampleDescription
NR21Outward codeNR21Identifies the postal district. 2-4 characters.
8ABInward code8ABIdentifies the specific sector and unit. Always 3 characters: digit + 2 letters.

The outward code itself has two parts:

PartNameExampleDescription
AreaPostcode areaNR1-2 letters. Identifies the postal area (e.g. NR = Norwich).
DistrictPostcode district211-2 digits (sometimes with a letter). Identifies the district within the area.

UK Postcode Examples

PostcodeAreaDistrictSectorUnitLocation
SW1A 1AASW1A1AABuckingham Palace, London
NR21 8ABNR218ABFakenham, Norfolk
EC1A 1BBEC1A1BBCity of London
BT1 1AABT11AABelfast, Northern Ireland
EH1 1AAEH11AAEdinburgh, Scotland
CF10 1AACF101AACardiff, Wales

Valid UK Postcode Formats

There are several valid postcode formats depending on the area code length and district format:

FormatExamplePattern
AN NAAE1 6ANSingle letter area, single digit district
ANN NAANR21 8ABSingle letter area, two digit district
AAN NAASW1A 1AASingle letter area, digit + letter district
AAN NAAEC1A 1BBTwo letter area, digit + letter district
ANN NAACF10 1AATwo letter area, two digit district
AN NAABT1 1AATwo letter area, single digit district

UK Postcode Validation Regex

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)

Normalising UK Postcodes

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'

Getting Postcode Data via API

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
}

Need postcode data in your application?

The Job Bookers Routing API converts UK postcodes to coordinates, drive times, distances and street names. Flat monthly pricing from £19/month.

View pricing

Flat monthly pricing. No surprises.

Your bill is the same every month regardless of how you use your allocation. No overage charges, ever.

Starter
£19/mo
1,000 calls/month
Get started
Business
£249/mo
50,000 calls/month
Get started
Enterprise
POA
Unlimited calls • Unlimited deployments
Contact us

More from Job Bookers Routing API

API Endpoints

Postcode to Lat Long Travel Time API Distance Matrix API Postcode API

Tools & Guides

Distance Between Postcodes Drive Time Calculator Delivery Checker UK Postcode Format Postcode Validation

Alternatives

Google Maps Alternative Ideal Postcodes Alternative Postcode Finder Address Finder

WordPress

WordPress Plugin WordPress Store Locator API Documentation Pricing