邮政编码基于有错误的数组

时间:2015-04-23 21:29:21

标签: php zipcode

我想开发一个邮政编码查找器,邮政编码由4位数字和2个字母组成。如果邮政编码正确,用户将收到我们在该地区提供的确认信息。如果没有,那就是我们不在那里传递的信息。

如果邮政编码不包含正确的标准,则输入错误:输入正确的邮政编码4位数字和2个字母。

这是php的粗略模型:

<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
    $postcode = array(6458,7493,6002,7520); 
    if(in_array($_POST['postcode'],$postcode))
    {
            echo 'We deliver on that district';
    }
    else
    {
        echo  'This order can not be delivered in that area';
    }
}


function IsPostcode($value) {
return preg_match('/^[1-9][0-9]{3} ?[a-zA-Z]{2}$/', $value);
}

if(IsPostcode($_POST['postcode'])) {
    echo 'Correct Zip code';
}
else {
    echo 'Incorrect zip code enter 4 letters and 2 numbers';
}

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" name="postcode" />
<input type="submit" value="verstuur" />
</form>

提前致谢

1 个答案:

答案 0 :(得分:0)

试一试

<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
    $postcode = array(6458,7493,6002,7520);
    if(preg_match('/^[1-9][0-9]{3} ?[a-zA-Z]{2}$/', $_POST['postcode'])) {
        if(in_array($_POST['postcode'],$postcode)) {
            echo 'We deliver on that district';
        } else {
            echo  'This order can not be delivered in that area';
        }
    } else {
        echo 'Incorrect zip code enter 4 letters and 2 numbers';
    }
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
    <input type="text" name="postcode" />
    <input type="submit" value="verstuur" />
</form>

首先应该检查它是否符合你的正则表达式,如果是,那么检查它们是否在你允许的范围内。您的正则表达式与当前允许的地址不匹配,因此所有这些地址都为This order can not be delivered in that areaIncorrect zip code enter 4 letters and 2 numbers

您也不需要只返回本机PHP函数结果的函数。

相关问题