Phone number validation in php codeigniter

2019-06-20 15:23发布

I have used the call back function for phone no validation from here.

function valid_phone_number_or_empty($value)
{
    $value = trim($value);

    if ($value == '') {
            return TRUE;
    }
    else
    {
            if (preg_match('/^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/', $value))
            {
                    return preg_replace('/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/', '($1) $2-$3', $value);
            }
            else
            {
                    return FALSE;
            }
    }
}

But now the problem is it doesn't accept phone-no from european countries and other parts of world which are 11 to 13 digits. Can anyone provide me with any other validation for that's universal for all countries?

3条回答
▲ chillily
2楼-- · 2019-06-20 16:00

This is always work for me:

$this->form_validation->set_rules('mobile', 'Mobile Number ', 'required|regex_match[/^[0-9]{10}$/]'); //{10} for 10 digits number
查看更多
女痞
3楼-- · 2019-06-20 16:03
$this->form_validation->set_rules('newphone','newphone', 
'required||matches[repassword]|max_length[10]|min_length[10]|xss_clean|
callback_isphoneExist');
查看更多
迷人小祖宗
4楼-- · 2019-06-20 16:12

take a look at the following. regex for international numbers. You will need to check against multiple regex. If the number does not match the first regex(us phone numbers) then check it against the international regex. If neither match, fail the test

查看更多
登录 后发表回答