A
A
artmirarmi2018-02-18 07:48:41
PHP
artmirarmi, 2018-02-18 07:48:41

How to make a regular expression to validate a number?

Hello.
I have a regular expression like this
/^((8|\+7|7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ] {7,10}$/
It checks the following numbers: 8(XXX)-XXX-YY-YY
How can I make it check landline numbers as well, for example, XX-XX-XX.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Antonio Solo, 2018-02-18
@solotony

either check for exact pattern matching with a simple regular expression, or it doesn’t make sense to suffer like this, because the spelling options are 100500: 8(86-141) 3-39-40 , +375 (44) 560-51-74 , so check what remains after removing brackets, spaces and hyphens

M
Mikhail Bobkov, 2018-02-18
@mike_bma

You can combine regular expressions for different types of numbers using |.
But here you need to take into account the possible different formats of numbers (there are landline numbers with 7 digits, etc.)
PS When he himself solved the problem of validating phone numbers, as a result, the validation was removed so that the valid number entered by the user does not mean that this number will be a real number and not a random dialing digits.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question