Validate UK short postcode

后端 未结 1 1789
醉酒成梦
醉酒成梦 2021-01-29 10:30

I\'m using this validator:

//validate postcode
function IsPostcode($postcode)
{
    $postcode = strtoupper(str_replace(\' \',\'\',$postcode));
    if(preg_match(         


        
相关标签:
1条回答
  • 2021-01-29 10:43

    you can use my updated regex to solve you problem

    it working from my end to validate UK zip code

    <?php 
    function IsPostcode($postcode)
    {
        $postcode = strtoupper(str_replace(' ','',$postcode));
        if(preg_match("/(^[A-Z]{1,2}[0-9R][0-9A-Z]?[\s]?[0-9][ABD-HJLNP-UW-Z]{2}$)/i",$postcode) || preg_match("/(^[A-Z]{1,2}[0-9R][0-9A-Z]$)/i",$postcode))
        {    
            return true;
        }
        else
        {
            return false;
        }
    }
    
    echo $result = IsPostcode('ME20');
    ?>
    

    OUTPUT

    1

    Hope this will sure help you.

    0 讨论(0)
提交回复
热议问题