A
A
alex stephen2015-04-03 15:44:55
MySQL
alex stephen, 2015-04-03 15:44:55

How to validate phone numbers?

Hello.
There is a list of phone numbers of users from VK.
As you know, 80% of users write anything in this field, but not a phone number, and the remaining 20% ​​write it in different formats. How can I select real numbers?
Numbers are only Russian, start with 7, +7, 8 or no prefix at all...
The number itself is 9 characters with a code. There can be any characters between characters (brackets, hyphens, spaces)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Tarakhonich, 2015-04-03
@berezuev

You need to cut off spaces, pros and cons. Then compare the length of the number and check that it consists only of digits. You can do something like this:

$phonenumber = '...';
$phonenumber = preg_replace('/(\-|\s|\+)/i', '', trim($phonenumber));
if (strlen($phonenumber) == 9 && preg_match('/^\d+$/i', $phonenumber)) {
    echo 'valid';
} else {
    echo 'invalid';
}

Of course, valid will only be if the person indicated the full number with the code.

T
trace8, 2018-03-14
@trase8

Here is another lib for validating phone numbers (php)
pakagist

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question