7

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?

Community
  • 1
  • 1
Sathya Raj
  • 1,079
  • 2
  • 12
  • 30

4 Answers4

25

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
vineet
  • 13,832
  • 10
  • 56
  • 76
  • 1
    Isnt that same as numeric|max_length[10] ? – Yash Jan 06 '17 at 09:49
  • There is more easy alternate of your solution. $this->form_validation->set_rules('mobile', 'Mobile Number ', 'required|max_length[10]|min_length[10]|greater_than[0]'); But in need little different solution. Can u help it? Is it possible to make first 3 digits restricted like 015, 016, 017, 018, 019 in a 10 digit number? All i need when input's first 3 digit do not match above format validation makes an error. Thanks. – Shiplu Jan 23 '17 at 07:20
3

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

Community
  • 1
  • 1
bretterer
  • 5,693
  • 5
  • 32
  • 53
  • ya the link you provided does had reference to international phone validation ,thanks for that .Then also i need replace reg-exp to separate between the international codes and and their number. – Sathya Raj May 31 '12 at 21:26
1

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

This solution uses International Telephone Input plugin.

Behold the steps:

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.
}

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

Serge Kishiko
  • 466
  • 1
  • 6
  • 13
-1
$this->form_validation->set_rules('newphone','newphone', 
'required||matches[repassword]|max_length[10]|min_length[10]|xss_clean|
callback_isphoneExist');
shin
  • 31,901
  • 69
  • 184
  • 271
sukumar v
  • 1
  • 1