【问题标题】:Phone number validation in php codeigniterphp codeigniter中的电话号码验证
【发布时间】:2012-06-06 00:25:08
【问题描述】:

我已经使用了来自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 位数的电话号码。任何人都可以向我提供所有国家通用的任何其他验证吗?

【问题讨论】:

    标签: php codeigniter validation phone-number


    【解决方案1】:

    这对我总是有用的:

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

    【讨论】:

    • 不是和 numeric|max_length[10] 一样吗?
    • 您的解决方案有更简单的替代方案。 $this->form_validation->set_rules('mobile', '手机号码', 'required|max_length[10]|min_length[10]|greater_than[0]');但需要一点不同的解决方案。你能帮忙吗?是否可以在 10 位数字中限制前 3 位数字,例如 015、016、017、018、019?当输入的前 3 位数字与上述格式验证不匹配时,我需要的只是一个错误。谢谢。
    【解决方案2】:

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

    【讨论】:

    • 你提供的链接确实提到了国际电话验证,谢谢。然后我还需要替换 reg-exp 以分隔国际代码和它们的号码。
    【解决方案3】:

    我有一个不需要 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.
    }
    

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

    【讨论】:

      【解决方案4】:
      $this->form_validation->set_rules('newphone','newphone', 
      'required||matches[repassword]|max_length[10]|min_length[10]|xss_clean|
      callback_isphoneExist');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-27
        相关资源
        最近更新 更多