How to validate a UK postal code in Java?

3.8k views Asked by At

One of the functions in the program is a user entering their postcode. So far I have:

if ( postcode != //SOMETHING// )
{
    JOptionPane.showMessageDialog(this, "Not UK Postcode", "Postcode must be a valid UK postcode", jOptionPane.ERROR_MESSAGE);
}

What would I put in the if statement in order for it to check if the entered postcode is a valid UK postcode?

3

There are 3 answers

1
Graham Rhind On

There are some special codes, such as GIR 0AA and SAN TA1 which haven't been mentioned in the formats previously given.

The letter J is never found as one of the first two letters. The letters Q, V and X are never found in the first position of the postal code. The letters I and Z are not found in the second position (except for the single postal code GIR 0AA ). Where the third position is a letter, the letters I, L, O, Q and Z are not found. Only the letters A, B, E, H, M, N, P, R, V, W, X and Y can appear in the fourth position. In the part of the postal code after the space, the letters C, I, K, M, O and V are never found.

A regex for validating current UK postal codes is:

\A((GIR 0AA)|(SAN TA1)|[A-I|K-P|R-U|W|Y-Z]0-9[0-9][A-B|D-H|J|L|N|P-U|W-Z][A-B|D-H|J|L|N|P-U|W-Z]|[A-I|K-P|R-U|W|Y-Z][0-9]0-9[0-9][A-B|D-H|J|L|N|P-U|W-Z][A-B|D-H|J|L|N|P-U|W-Z]|[A-I|K-P|R-U|W|Y-Z][0-9]A-H|J-K|M-N|P|R-Y[0-9][A-B|D-H|J|L|N|P-U|W-Z][A-B|D-H|J|L|N|P-U|W-Z]|[A-I|K-P|R-U|W|Y-Z][A-H|K-P|R-U|W|Y]0-9[0-9][A-B|D-H|J|L|N|P-U|W-Z][A-B|D-H|J|L|N|P-U|W-Z]|[A-I|K-P|R-U|W|Y-Z][A-H|K-P|R-U|W|Y][0-9]0-9[0-9][A-B|D-H|J|L|N|P-U|W-Z][A-B|D-H|J|L|N|P-U|W-Z]|[A-I|K-P|R-U|W|Y-Z][A-H|K-P|R-U|W|Y][0-9]A-H|J-K|M-N|P|R-Y[0-9][A-B|D-H|J|L|N|P-U|W-Z][A-B|D-H|J|L|N|P-U|W-Z])\Z

You can find more information on UK postal codes at http://www.grcdi.nl/gsb/united%20kingdom.html

1
Al Mills On

There is lots of information around on this already including:

But taking those into account, there are 6 possible formats for postcodes in the UK:

A9 9AA
A9A 9AA
A99 9AA
AA9 9AA
AA9A 9AA
AA99 9AA

And there are several way's that you can validate them - you'd need to give a bit more information to explain exactly what you are after.

Syntax Validation

We can use a regex to validate that a postcode looks like a postcode. Something along the lines of the following should work. It allows for the 6 values. There are more complicated / complete examples online, again - depending upon what you are after,.

^[A-Z]{1,2}[0-9R][0-9A-Z]?[0-9][ABD-HJLNP-UW-Z]{2}$

Validation

Just because the postcode looks like a postcode, doesn't mean that it is a postcode. If you are trying to capture a complete address or ensure that the postcode entered by a user actually exisits you could use the Royal Mail PAF file or a 3rd party to help you capture a complete & correct address (many available including http://www.qas.co.uk/knowledge-centre/product-information/address-postcode-finder.htm (my company)).

0
ram On
[a-z A-Z 0-9]*{9} 

iused this as validating postalcode not for UK but for all type of country postalcodes .it worked for me.