I was asked by someone to code up a system that ensures a UK postcode goes into the DB perfectly formatted - the task was not to ensure that the postcode actually exists, simply to write it into the DB in a particular format:
1 or 2 letters
followed by
1 or 2 numbers
followed by a space
followed by 1 number
followed by 2 letters
and all in Uppercase.
Examples (of correctly formatted postcode):
AB1 2CD
MN23 4NW
E2 1TG
Here is the Problem (incorrectly formatted postcode):