Validate a UK postcode

You’ve probably seen this before, if not a thousand times, but here’s a simple function to check the format of a UK postcode.

function valid_uk_postcode($postcode)
{
    return (preg_match('/^([A-PR-UWYZ][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {0,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/i', $postcode)) ?
        true : false;
}
Did you like this? Share it:

Leave a Reply