Validating postcodes php

These functions check the validity of a UK postcode specified by the supplied parameter. The definition of a valid postcode has been taken from the official specification, and from the list of BFPO codes.

The functions also take into account which letters are valid in the various positions.

:) The original version I found was a little out of date as it used the old ereg PHP regular expression functions, which caused deprecated errors in PHP 5.3.

At the time I was creating a search feature that used full and partial postcodes so I also changed the function to be able to detect both. Note that the postcode is passed by reference so if a postcode is found it is standardised into a correct format.

Creating a function to validate UK postcodes would seem like a simple task, but there is a little more to it than checking the number of characters.

In fact there are several different variants of UK postal codes, especially if you include BFPO and overseas addresses.

It's easy enough to add a filter to remove the space, but I'm a little worried that when (and if) it gets fixed, will the fixed version Zend_Validate_Post Code then fail to validate postcodes without the space?

In theory it should as the space is part of the spec.

Download the two files below and upload them to your web server.

But to be honest, even with London postcodes only, you need a lot more storage than necessary. Store the postcodes, take the user input or whatever, and see if you get a match.

But you are complicating the solution far more than you think.

It would only take the most basic regex knowledge to work out your answer from the starting point you've already got.

I suggest going and reading up a bit more about regex rather than asking such a basic question.

Search for validating postcodes php:

validating postcodes php-32validating postcodes php-60validating postcodes php-61validating postcodes php-82

The database is only required for the get Post Town function.

Leave a Reply

Your email address will not be published. Required fields are marked *

One thought on “validating postcodes php”