Valid UK Postcode Formats

We are often required to validate input fields. One field that is often neglected or relies heavily on users inputting data in the correct format is the UK postcode field. The format of the postcode is listed here.

We have a Regular Expression / Javascript solution that validates UK postcode formats. Briefly all valid formats are:

 

Format                             Example
AN NAA                          M1 1AA
ANN NAA                      M60 1NW
AAN NAA                      CR2 6XH
AANN NAA                   DN55 1PT
ANA NAA                       W1A 1HQ
AANA NAA                   EC1A 1BB

 

The regex we use is:

/^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/i

Or you could try this:

^((([A-PR-UWYZ][0-9])|([A-PR-UWYZ][0-9][0-9])|([A-PR-UWYZ][A-HK-Y][0-9])|([A-PR-UWYZ][A-HK-Y][0-9][0-9])
|([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))\s?([0-9][ABD-HJLNP-UW-Z]{2})|
(GIR)\s?(0AA))$

Simple and effective.

UDPATE.  This will only validate UPPERCASE input. You will need to upper your input before validating it.

Comments

0 Leave a comment