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 PostcodeLab · Published · Updated
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.
| Part | Purpose | Example |
|---|---|---|
| Area | Broad postal area | PO |
| District | Subdivision of the area | PO15 |
| Sector | First inward-code digit | PO15 5 |
| Unit | Full postcode unit | PO15 5RR |
Normalise before validating
- Trim leading and trailing whitespace.
- Convert letters to upper case for storage or comparison.
- Remove harmless punctuation only if your product has documented that behaviour.
- 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
Related developer guides
UK Postcode Regex and Validation Examples
Build a maintainable UK postcode regex with normalisation, JavaScript examples, valid format cases, server-side checks and clear validation limits.
Read guidePostcode Validation vs Address Verification
Compare postcode syntax validation, postcode lookup, address verification, UPRN matching and deliverability checks when designing UK forms and tests.
Read guideUK Address Form Test Cases and QA Checklist
Use practical UK address form test cases for field variation, postcodes, lookup failure, accessibility, mobile layouts, exports and integration safety.
Read guideUK Address Format: Correct Order and Examples
See the correct UK address format for houses, flats and named buildings, with postcode placement and line-by-line examples for UK and international use.
Read guide