Developer Guide

UK Postcode Validation

How to validate UK postcodes in your application — regex patterns, edge cases, normalisation, and API-based validation.

The Standard Validation Regex

This regex handles all standard UK postcode formats:

/^[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}$/i

It accepts postcodes with or without a space, in upper or lower case, and covers all UK postcode area formats.

Validation in JavaScript

function isValidUKPostcode(postcode) {
  const regex = /^[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}$/i;
  return regex.test(postcode.trim());
}

isValidUKPostcode('NR21 8AB');   // true
isValidUKPostcode('SW1A 1AA');   // true
isValidUKPostcode('invalid');    // false
isValidUKPostcode('NR218AB');    // true (no space)

Validation in Python

import re

def is_valid_uk_postcode(postcode):
    pattern = re.compile(r'^[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}$', re.IGNORECASE)
    return bool(pattern.match(postcode.strip()))

is_valid_uk_postcode('NR21 8AB')  # True
is_valid_uk_postcode('ZZ99 9ZZ')  # False

Validation in PHP

function is_valid_uk_postcode(string $postcode): bool {
    $pattern = '/^[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}$/i';
    return (bool) preg_match($pattern, trim($postcode));
}

is_valid_uk_postcode('NR21 8AB');  // true
is_valid_uk_postcode('not valid'); // false

Normalising Before Validation

Always normalise a postcode before validating — strip whitespace, uppercase, and ensure correct spacing:

function normalisePostcode(raw) {
  const p = raw.toUpperCase().replace(/\s+/g, '').trim();
  return p.slice(0, -3) + ' ' + p.slice(-3);
}

// Then validate the normalised version
const postcode = normalisePostcode(userInput);
if (isValidUKPostcode(postcode)) {
  // safe to use
}

Edge Cases to Handle

InputIssueNormalised
nr21 8abLowercaseNR21 8AB
NR218ABNo spaceNR21 8AB
NR21 8AB Leading/trailing spacesNR21 8AB
NR21 8ABDouble spaceNR21 8AB
BT1 1AANorthern Ireland (BT prefix)BT1 1AA

API-Based Validation

For production applications, validate postcodes against a live dataset rather than just regex. The Job Bookers Routing API returns a 400 invalid_postcode error for malformed postcodes and a 404 not_found error for postcodes that don't exist in the dataset — giving you both format and existence validation in one call.

GET /v1/geocode?postcode=ZZ99+9ZZ

{
  "error": {
    "code": "invalid_postcode",
    "message": "Not a valid UK postcode."
  }
}

Validate postcodes and get routing data in one API

The Job Bookers Routing API validates UK postcodes and returns coordinates, drive times and street names. 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