Validating postcodes php
:) 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.
Fortunately, there are libraries and services available which will perform these two tasks without you needing to fully understand the postcode system.
Validating a postcode is a case of checking that the postcode is in the correct format, e.g. The best library I have found for this is Braemoor Software’s Postcodes Validation Script, which contains PHP and Java Script code so you can validate on the client side and the server side.
Google API (free but rate limited) or a paid for service like Postcode anywhere will be much better for you The OP's regex doesn't require there to be a number, so it will match against an address even if it doesn't contain a number (other than the space). There are two problems, the first one the \d, because a zero is not allowed as the first number and secondly the check using the \s.
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.
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.