[英]International Phone number Validation with whitespace
我正在使用codeigniter验证库...对于一个国际号码我试图接受一个数字15位数...也允许白色空间shud ..但下面由于某种原因不起作用...它不接受空白.. 。
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中使用它来制作回调函数..? 或者制作一个我自己的正则表达式? 任何投入都赞赏
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;
}
//在表单验证规则中
$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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.