L
L
lid2pro2022-04-19 17:06:20
PHP
lid2pro, 2022-04-19 17:06:20

Regular expression to validate phone number in PHP?

Hello,
can you please tell me how to fix this.

if (preg_match('/((8|\+7)-?)?\(?\d{3,5}\)?-?\d{1}-?\d{1}-?\d{1}-?\d{1}-?\d{1}((-?\d{1})?-?\d{1})?/', $login_phone)) {
...
}

so that the phone number is checked strictly according to the format +7 (999) 999-99-99

Thank you in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2022-04-19
@lid2pro

if (preg_match('/^\+7 \(\d{3}\) \d{3}-\d{2}-\d{2}$/', $login_phone)) {
    echo 'ok';
} else {
  echo 'wrong number';
}

PHP preg_match

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question