UK postcode regex and snippet generator
Choose your form, pick a validation policy and copy a tested starting point. Check the format without sending a postcode anywhere.
This preview checks the helper. WooCommerce may clean spaces and hyphens before running the filters.
This preview normalises input. HTML checks what was typed, with an optional single space.
PHP examples omit the opening <?php tag. Add it in a standalone PHP file; omit it in a snippet manager. Install the helper function once. The PHP, JavaScript and Gravity Forms examples validate without changing the submitted value. The WooCommerce example can also repair native BFPO spacing.
Set the form and field IDs in gform_field_validation_2_1. The example checks the ZIP / postcode sub-input of an Address field (or a single text field). Keep your field’s required setting. Use it on a UK-only form.
Classic checkout example: these shared filters replace WooCommerce’s GB postcode format rule and can affect other WooCommerce postcode checks. Other countries and blank-value validation results are unchanged; required-field and shipping rules stay separate. WooCommerce may remove spaces and hyphens before validation. BFPO references are repaired after its native spacing when enabled. The Checkout Block flow is not verified. Test on staging before using it on a store.
GIR 0AA, BF1 postcodes and JE / GY / IM formats are included in both policies. No regex can confirm a real address or your delivery coverage.
See the shared regex body
Use the full snippet for its anchors and case handling. The HTML pattern applies to the whole input automatically.
A postcode regex is a format check
Strict validation uses the published letter-position restrictions. Lenient validation keeps the broad postcode shape but allows any letter A–Z. For example, ZZ99 9ZZ passes the lenient policy and fails the strict one. Neither policy checks allocation or deliverability.
For customer forms, consider the cost of blocking a visitor. Keep a way to enter an address manually, validate on the server as well, and use postcode lookup when you need a complete address.
Adding the snippet to WordPress
Use a small site plugin or a snippet manager, test on staging and keep the shared helper only once. The Gravity Forms example uses the gform_field_validation filter. The WooCommerce example uses the shared postcode-validation filter.
For address lookup, see the Gravity Forms plugin or the WooCommerce Checkout Block plugin.
Questions about this tool
- Should I use strict or lenient postcode validation?
- Strict validation catches disallowed letters in specific positions. Lenient validation checks the broad shape and is less likely to block an unusual postcode, but accepts more typos. Neither proves that a postcode exists. Choose based on the cost of rejecting a customer versus accepting a typo.
- Are these snippets an address lookup?
- No. They check text only and make no API calls. Use postcode lookup to let a visitor select a complete address.
- Does the WooCommerce snippet support the Checkout Block?
- This example uses shared WooCommerce postcode filters, including the classic checkout flow. It replaces the GB format rule and can affect other WooCommerce postcode checks. Required-field and shipping rules stay separate. WooCommerce may clean spaces and hyphens first. The Checkout Block flow is not verified; use the BePostcodes Checkout Block plugin for address lookup.
- Can I rely on an HTML pattern alone?
- No. Browser validation helps visitors, but it can be bypassed. Validate again on your server. The HTML example accepts upper or lower case and an optional single space; PHP and JavaScript also trim surrounding whitespace and collapse repeated spaces.
Sources and scope
Letter restrictions and the GIR 0AA exception use the published UK Post Code entry in the CJS Data Standards Catalogue, section 5.25, with P also accepted in an A9A outward code: Camden’s official postcode dataset includes N1P 3AJ. This is a format policy, not a directory of allocated postcodes.
GOV.UK lists BFPO numbers alongside their BF1 postcodes. A BFPO number is a separate reference; this tool does not confirm that a number is assigned.
JE, GY and IM are flagged as Crown Dependencies: Jersey, Guernsey and the Isle of Man. See GOV.UK’s Crown Dependencies overview and the ONS postcode directory user guide. Check delivery coverage separately.
Last reviewed: 1 December 2026