微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

来自不同国家的邮政编码验证

如何解决来自不同国家的邮政编码验证

在这样的网站上进行了验证检查

'username' => array('required' => true,'min' => 2,'max' => 50,'unique' => 'users'),

因此需要用户名用户名最少2个,最多50个字符,并且在“用户”表中唯一。

我想添加邮政编码。问题是,它因国家/地区而异。该国家处于选择状态:

<select class="select" name="land" id="land" onchange="if (this.selectedindex) land();">
  <option value="-1">--</option>
  <option value="c1">Country 1</option>
  <option value="c2">Country 2</option>
  <option value="c3">Country 3</option>
</select>

在JS代码中,我有这个:

function land(){
    
    $("select.land").change(function() {
        var selectedCountry = $(this).children("option:selected").val();
        if(selectedCountry === 'Belgie'){
            <?PHP $postalCode = "'postcode' => array('required' => true,'figures' => true,'max' => 4,'notlowercase' => true,'notuppercase' => true,'notspecial' => true)," ?>
        }else if(selectedCountry === 'Nederland'){
            <?PHP $postalCode = "'postcode' => array('required' => true,'max' => 7,'uppercase' => true," ?>
        }
    });
}

问题是,如果我仅在验证中输入$ postalCode,就像这样:

'username' => array('required' => true,$postalCode,...

然后PHP在foreach上给了我一个无效的参数,它的开头是这样的:

foreach($rules as $rule => $rule_value) {

问题是,我该如何解决

--------更新----------- 现在我有了这个:

$land = $_POST['land']; 
if($land === 'Belgie' || $land === 'belgie'){ 
  $test = "'postcode' => array('required' => true,"; 
}else if($land === 'Nederland' || $land === 'nederland'){ 
  $test = "'postcode' => array('required' => true,'max' => 6,"; 
} 

但仍然是相同的错误(参数无效)。我认为这是因为我提供了一个String并且期望使用Array,但不知道如何解决这个问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。