An Entrepreneur, Coach, IT Consultant, Strategic Adviser, and a Traveler craving to explore and contribute to forming a better society.

Wednesday, March 31, 2010

How to Validate UK Postal Code using Regular Expression?

1 comment :


Regular Expression for Valid UK Postcode/Postal Code

Problem/Question:

Validating UK postal code is always complex. In general, the validation will be for spaces, number of characters + minimum and maximum length of the post code.

What is the best way of validating an UK Postal Code (PostCode). Also, that should help us splitting the outer and inner postal code to use it in a much better way.

Solution:

#Regular Expression 1 – To validate/match valid UK Postal Code

^(GIR[ ]?0AA)$|^([A-PR-UWYZ][0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][0-9][0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][A-HK-Y0-9][0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][A-HK-Y0-9][0-9][0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][0-9][A-HJKS-UW0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$|^([A-PR-UWYZ][A-HK-Y0-9][0-9][ABEHMNPRVWXY0-9][ ]?[0-9][ABD-HJLNPQ-UW-Z]{2})$

#Regular Expression 2 – To validate/match and Group Outer/Inner Postcode of a valid UK Postal Code

^((GIR)[ ]?(0AA))$|^(([A-PR-UWYZ][0-9])[ ]?([0-9][ABD-HJLNPQ-UW-Z]{0,2}))$|^(([A-PR-UWYZ][0-9][0-9])[ ]?([0-9][ABD-HJLNPQ-UW-Z]{0,2}))$|^(([A-PR-UWYZ][A-HK-Y0-9][0-9])[ ]?([0-9][ABD-HJLNPQ-UW-Z]{0,2}))$|^(([A-PR-UWYZ][A-HK-Y0-9][0-9][0-9])[ ]?([0-9][ABD-HJLNPQ-UW-Z]{0,2}))$|^(([A-PR-UWYZ][0-9][A-HJKS-UW0-9])[ ]?([0-9][ABD-HJLNPQ-UW-Z]{0,2}))$|^(([A-PR-UWYZ][A-HK-Y0-9][0-9][ABEHMNPRVWXY0-9])[ ]?([0-9][ABD-HJLNPQ-UW-Z]{0,2}))$

Note: The above regular expression will validate and group a valid UK postcode as follows:

Example: E14 9GE
Group Case 1: E14 9GE
Group Case 2: E14        //Outer UK Post Code
Group Case 3: 9GE        //Inner UK Post Code

#Regular Expression 3: To Group only Inner & Outer UK Postal Code and Validate


^(GIR)[ ]?(0AA)$|^([A-PR-UWYZ][0-9])[ ]?([0-9][ABD-HJLNPQ-UW-Z]{0,2})$|^([A-PR-UWYZ][0-9][0-9])[ ]?([0-9][ABD-HJLNPQ-UW-Z]{0,2})$|^([A-PR-UWYZ][A-HK-Y0-9][0-9])[ ]?([0-9][ABD-HJLNPQ-UW-Z]{0,2})$|^([A-PR-UWYZ][A-HK-Y0-9][0-9][0-9])[ ]?([0-9][ABD-HJLNPQ-UW-Z]{0,2})$|^([A-PR-UWYZ][0-9][A-HJKS-UW0-9])[ ]?([0-9][ABD-HJLNPQ-UW-Z]{0,2})$|^([A-PR-UWYZ][A-HK-Y0-9][0-9][ABEHMNPRVWXY0-9])[ ]?([0-9][ABD-HJLNPQ-UW-Z]{0,2})$

Example: E14 9GE
Group Case 1: E14        //Outer UK Post Code
Group Case 2: 9GE        //Inner UK Post Code

To test the above regular expressions use the following javascript regular expression test:

http://www.regular-expressions.info/javascriptexample.html


Explanation of the above Regular Expresssions:

There are six possible formats for UK postcodes/postalcodes.
#No    Format      Example Postcode
1      AN NAA      M1 1AA
2      ANN NAA     M60 1NW
3      AAN NAA     CR2 6XH
4      AANN NAA    DN55 1PT
5      ANA NAA     W1A 1HQ
6      AANA NAA    EC1A 1BB
 Please note the following:-
  • The letters Q, V and X are not used in the first position.
  • The letters I, J and Z are not used in the second position.
  • The only letters to appear in the third position are A, B, C, D, E, F, G, H, J, K, S, T, U and W.
  • The only letters to appear in the fourth position are A, B, E, H, M, N, P, R, V, W, X and Y.
  • The second half of the Postcode is always consistent numeric, alpha, alpha format and the letters C, I, K, M, O and V are never used.
  • These conventions may change in the future if operationally required.
*GIR 0AA is a Postcode that was issued historically and does not confirm to current rules on valid Postcode formats; It is however, still in use.

For more information:
http://www.cabinetoffice.gov.uk/govtalk/schemasstandards/e-gif/datastandards/address/postcode.aspx