国际电话号码验证用空格

时间:2013-02-28 03:36:45

标签: php regex codeigniter

我正在使用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中使用它来制作回调函数..?或者制作一个我自己的正则表达式?任何投入赞赏

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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;
    }
  }
相关问题