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?
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:
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,.
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)).