Postcode validation and verification

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 (update: as of February 2022 the script is no longer available).

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.

2 Comments

  1. Robin Lucas

    Braemoor Software link returns this error –>

    Gone

    The requested resource is no longer available on this server and there is no forwarding address. Please remove all references to this resource.

    Additionally, a 410 Gone error was encountered while trying to use an ErrorDocument to handle the request.

    • Paul

      Thanks for spotting this. Unfortunately it looks like the script has completely disappeared so I’ve removed the link and updated the post to reflect this.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.