Validating United Kingdom postcodes

Q

How to write a regular expression to validate United Kingdom postcodes?

For example, EC1A 1BB is a valid UK postcodes.

✍: Guest

A

Here is the regular expression to validate United Kingdom postcodes:

[A-Z]{1,2}\d[A-Z\d]? \d[ABD-HJLNP-UW-Z]{2}

[A-Z]{1,2}            - 1 or 2 letters
\d                    - A digit
[A-Z\d]?              - An optional letter or digit
                      - A space
\d                    - A digit
[ABD-HJLNP-UW-Z]{2}   - Any 2 letters except CIKMOV 

Click the button to test this regular expression here online:

2013-01-29, 2044👍, 0💬