0

我正在使用 codeigniter 验证库...对于国际号码,我正在尝试接受 15 位数字..也允许使用空格..但由于某种原因下面不起作用...它不接受空格.. .

          if(isset($data['perfume_int_contact']))
           {
            //$this->form_validation->set_rules('phone_int_area_code','Contact Phone Number','trim|required|numeric');

            $this->form_validation->set_rules('phone_int_first','Contact Phone Number','trim|required|numeric');                        

我应该能够像这样输入:1234 1234 1234 45678 我应该为此创建自己的验证类吗?或者我可以在 set_rules.. 中使用它来创建一个回调函数吗?或者做一个我自己的正则表达式?任何输入表示赞赏

4

2 回答 2

1
if (isset($data['perfume_int_contact'])) {
    $this->form_validation->set_rules('phone_int_first', 'Contact Phone Number', 'trim|numeric|required|callback_phone_check');
}

function phone_check($phone_number)
{
    $regex = '/^\d{3}\s\d{3}\s\d{4}\s\d{3}$/'; // validates 123 123 1234 123
    if (!preg_match($regex, $phone_number)) {
        $this->form_validation->set_message('phone_check', 'Phone Number not valid.');
        return false;
    }
    return true;
}
于 2013-02-28T03:46:58.757 回答
1

// 在表单验证规则中

$this->form_validation->set_rules('phone_int_first', 'Contact Phone Number', 'trim|required|max_length[15]|callback_checkPhone');

// 回调函数

  function checkPhone($phoneNumber) {
    $output = preg_match('/[^0-9\s]/', $phoneNumber);
    if (empty($output)) {
      return TRUE;
    } else {
      $this->form_validation->set_message('checkPhone', 'This phone number conatins only numbers & white space');
      return FALSE;
    }
  }
于 2013-02-28T03:53:14.677 回答