-2

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?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Bluestinct
  • 11
  • 1
  • 3
  • Well, do you have a list of valid UK post codes? Is it in a database? In a text file? Available via a web service? Provided in installments by messenger pigeons? Really, you need to think about how this should work, before you start writing code. – Dawood ibn Kareem Nov 25 '14 at 18:39
  • 1
    Have you done any research on what is a valid postcode? – Jimmy T. Nov 25 '14 at 18:39
  • 1
    possible duplicate of [Regular expression for UK postal codes](http://stackoverflow.com/questions/14998459/regular-expression-for-uk-postal-codes) – Kick Buttowski Nov 25 '14 at 18:39
  • @DavidWallace Nope, just literally making sure they cant enter something like "BFUWIDAPBVUIWDP" in the postcode field – Bluestinct Nov 25 '14 at 18:41
  • @KickButtowski Not quite a duplicate. If we assume that the OP intends to match the candidate post code against the regular expression provided in the linked question, he/she still doesn't have the code to actually do the match. – Dawood ibn Kareem Nov 25 '14 at 18:41
  • @Bluestinct - That's even less clearly defined. What does _something like "BFUWIDAPBVUIWDP"_ actually mean? – Dawood ibn Kareem Nov 25 '14 at 18:42
  • @DavidWallace Say you went to enter your postcode in the postcode field. Instead of adding your actual postcode, you just entered a jumble of numbers and/or letters; it needs to bounce an error. – Bluestinct Nov 25 '14 at 18:45
  • So what is the defining feature that makes you consider "BFUWIDAPBVUIWDP" a "jumble of numbers and/or letters", and "CR9 5AD" not? – Dawood ibn Kareem Nov 25 '14 at 18:57
  • I suppose I could check for the length? – Bluestinct Nov 25 '14 at 19:06
  • You could. So is your question "how do I check whether a String is either 7 or 8 characters long, and show a message if it's a different length"? – Dawood ibn Kareem Nov 25 '14 at 19:26

3 Answers3

5

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

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

Community
  • 1
  • 1
Al Mills
  • 1,072
  • 6
  • 22
  • For me, the reg ex only worked in Java 11 with a space after the question mark in the middle: `^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}$` – Karsten Silz Mar 11 '20 at 18:04
0
[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.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
ram
  • 1