If you run a UK website which collects any sort of address information, you will no doubt have come across the potentially thorny subject of postcodes. Specifically, you will want to perform the two basic checks required for most user input: validation and verification. Fortunately, there are libraries and services available which will perform these two tasks without you needing to fully understand the postcode system.
Validation
Validating a postcode is a case of checking that the postcode is in the correct format, e.g. BL9 9BB is a valid postcode but BBB 9999 is not. The best library I have found for this is Braemoor Software’s Postcodes Validation Script, which contains PHP and JavaScript code so you can validate on the client side and the server side.
Verification
Verification is the process by which you check whether a postcode actually exists. For example, BL1 1ZZ will pass the validation test as it matches one of the possible postcode formats, but it will fail the verification test because it is not a real postcode. By far the easiest free service for verifying postcodes is MaPit, which is run by the excellent folk at mySociety. You can either query their service directly, or download the code and data to run your own copy. If you don’t want to write your own code to access the service, you can use my mapit-lookup class to perform basic postcode verification.