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

时间:2020-09-05 11:56:36

标签: php jquery select foreach

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

'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,'figures' => true,'max' => 7, 'notlowercase' => true, 'uppercase' => true, 'notspecial' => true)," ?>
        }
    });
}

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

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

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

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

问题是,我该如何解决?

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

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

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

0 个答案:

没有答案