Skip navigation |

Postcode Validation

To add a field that validates someone's postcode, follow these steps to ensure that the user can only enter the postcode in a valid format.

Last updated: 09 May 2012, 15:55

The official Postcode Specification states that a valid UK postcode conforms to the following rules:

  1. The letters Q, V and X are not used in the first position.
  2. The letters I, J and Z are not used in the second position.
  3. 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.
  4. The only letters to appear in the fourth position are A, B, E, H, M, N, P, R, V, W, X and Y.
  5. 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.

Note: These conventions may change in the future if operationally required.

If you want users of your site to supply their postcode, you can ensure that they can only enter a valid one by doing the following.

Add a Single Line Text item and add the following, exactly as shown, to the Parameters box:

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$

The above code gives users the option to enter the postcode in either upper or lower case lettering. If you want to force users to enter the postcode in upper-case only, add the following text to the Parameters box:

^(GIR 0[A]{2})|((([A-Z][0-9]{1,2})|(([A-Z][A-HJ-Y][0-9]{1,2})|(([A-Z][0-9][A-Z])|([A-Z][A-HJ-Y][0-9]?[A-Z])))) {0,1}[0-9][A-Z]{2})$

Note: The validation code matches postcodes starting GIR which are for GiroBank.

 

Cookies are used by nearly every website to improve your web browsing experience. Recent government legislation (the e-Privacy Directive) means that we are obliged to seek your agreement to the use of cookies when you visit our website. Two of the cookies that we use are essential for this website to operate and have already been set. You may delete and block all cookies from this site, but if you do so, parts of the site will not work. To find out more about the cookies that we use, please see our privacy policy.