UK postcode testing

UK Postcode Format and Validation Guide

A UK postcode is more than a generic string of letters and numbers. Developers need to distinguish formatting, syntactic validation and confirmation that a postcode currently exists.

By · Published · Updated

UK Postcode Format and Validation Guide visual guide cover

Choose the postcode task you need

Use the random UK postcode generator when you need an exploratory postcode-first result, the UK postcode checker and formatter when you already have an input value, and the UK postcode validation test suite for repeatable automated assertions. Each solves a different task; none alone proves deliverability or an identity relationship.

Outward code and inward code

The Office for National Statistics describes a postcode as an outward code of two to four characters followed by a three-character inward code. In the ONS example PO15 5RR, PO15 is the outward code and 5RR is the inward code.

PartPurposeExample
AreaBroad postal areaPO
DistrictSubdivision of the areaPO15
SectorFirst inward-code digitPO15 5
UnitFull postcode unitPO15 5RR

Normalise before validating

  1. Trim leading and trailing whitespace.
  2. Convert letters to upper case for storage or comparison.
  3. Remove harmless punctuation only if your product has documented that behaviour.
  4. Insert one space before the final three characters.

The GOV.UK address pattern recommends accepting lower case, missing spaces and additional spaces rather than making users correct superficial formatting themselves.

Syntax is not existence

A regular expression can reject an impossible shape, but it cannot prove that Royal Mail currently recognises a postcode. Postcodes can be introduced, terminated or reused in datasets over time. Use an authoritative lookup when your workflow needs current existence, and decide whether terminated postcodes are valid for historical records.

The ONS Postcode Directory links current and terminated postcodes to geographic areas and is released quarterly. A delivery or identity decision may require a different, licensed address source.

Useful QA cases

  • One-letter and two-letter postcode areas.
  • Outward codes containing a final letter.
  • Lower-case input and input without a space.
  • Valid syntax that is not found by the chosen lookup service.
  • Terminated postcodes in historical data.
  • Server-side validation when client-side validation is bypassed.

Official references