Creative Jar Blog RSS Feed

Valid UK Postcode formats

May 20, 2008 12:24 by gareth

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 http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm.

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

FormatExample
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.

 

 


Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

August 28. 2008 13:50

It is not validating CV10 8QS post code

sunny

Add comment


 

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

January 6. 2009 18:37

Calendar

January 2009
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567