Answer the question
In order to leave comments, you need to log in
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
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';
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question