php codeigniter中的电话号码验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10838804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:11:07  来源:igfitidea点击:

Phone number validation in php codeigniter

phpcodeignitervalidationphone-number

提问by Sathya Raj

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})$/', '() -', $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?

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

采纳答案by bretterer

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

看看下面的内容。 国际号码的正则表达式。您将需要检查多个正则表达式。如果该号码与第一个正则表达式(美国电话号码)不匹配,则根据国际正则表达式进行检查。如果两者都不匹配,则测试失败

回答by vineet

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

回答by Serge Kishiko

I have a solution doesn't need CodeIgniter form rules. But it works perfectly in my CodeIgniter project.

我有一个解决方案不需要 CodeIgniter 表单规则。但它在我的 CodeIgniter 项目中完美运行。

This solution uses International Telephone Inputplugin.

此解决方案使用国际电话输入插件。

Behold the steps:

看步骤:

views.php

视图.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

控制器.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.
}

There you go! Your model must be the same, it doesn't need any change.

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

回答by sukumar v

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