php codeigniter中的电话号码验证

时间:2012-05-31 18:01:37

标签: php codeigniter validation phone-number

我已使用回拨功能进行电话验证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;
            }
    }
}

但现在的问题是它不接受来自欧洲国家和世界其他地区的11到13位数的电话号码。任何人都可以为我提供任何其他所有国家的通用验证吗?

4 个答案:

答案 0 :(得分:17)

这对我来说总是有用的:

$this->form_validation->set_rules('mobile', 'Mobile Number ', 'required|regex_match[/^[0-9]{10}$/]'); //{10} for 10 digits number

答案 1 :(得分:3)

看看以下内容。 regex for international numbers。您需要检查多个正则表达式。如果该号码与第一个正则表达式(我们的电话号码)不匹配,则将其与国际正则表达式进行核对。如果两者都不匹配,则测试失败

答案 2 :(得分:0)

我有一个不需要CodeIgniter表单规则的解决方案。但这在我的CodeIgniter项目中可以很好地工作。

此解决方案使用International Telephone Input插件。

遵循步骤:

views.php

<link rel="stylesheet" href="<?= base_url("resources/css/intl-tel-input/intlTelInput.css");?>">
<!-- Somewhere in your code -->
<input name="phone" id="phone" class="form-control" type="phone" required>

<!-- Your scripts place -->
<script src="<?= base_url("ressources/js/intl-tel-input/intlTelInput.js"); ?>"></script>
<script>
    // Initialization
    var input = document.querySelector("#phone");
    var intl = window.intlTelInput(input);
    // Assuming that the submit button of your form is IDed by '#quote_form'
    $('#quote_form').submit(function(event) {
        var input = $('#phone');
        // My custom check
        if ( (input.val().length == 10 && input.val().startsWith('0')) ||
            (input.val().length == 9 && !input.val().startsWith('0'))) {
           // Here, before submitting the form, I changed the input value 
           // by what the plugin provides 
           // (which include the country code and the without-zero phone number)
           // The value may be +24381234567
           input.val(intl.getNumber());
        }
        else {
            // In the case when the user types an invalid phone number
            // But you can do more by displaying a error message
            event.preventDefault();
        }
    });
</script>

controller.php

// Somewhere in your controller code...
if (isset($_POST['request'])) {
    // HERE IS THE INTERESTING PART OF THIS SOLUTION
    // You don't need any extra rules to validate the phone number value
    // No need a regex that supports all the country codes
    // As when that code comes in, it's valided and support all the country code
    $this->form_validation->set_rules('phone', 'Numéro de téléphone', 'required');
    // You can continue here with your app code.
}

您去了!您的模型必须相同,不需要任何更改。

答案 3 :(得分:-1)

$this->form_validation->set_rules('newphone','newphone', 
'required||matches[repassword]|max_length[10]|min_length[10]|xss_clean|
callback_isphoneExist');