TheChampp
TheChampp

Reputation: 1437

Phone number regex

I need regular expression for phone number.

(from 0 to 9) (total 10 digits)
Example: "0123456789"

Upvotes: 1

Views: 2408

Answers (3)

Adrian Salazar
Adrian Salazar

Reputation: 5319

Normally a \d{10} would do the trick. But that is the lazy-man's way of validating as '0000000000' would pass a a valid entry.

But in case you know more about the domain, and the rules (I mean, if your phone number belongs to a specific country) and you want to make sure the number matches the local rules, then you can be a little bit more specif.

For example if all the numbers are starting with a leading zero, you can do this

0\d{9}

Or if the prefixes are well know... you can make an expression that allows phone numbers only starting with certain prefix(es).

(017|016|019|012)\d{7}

This will allow only those prefixes in the list, plus other 7 digits.

Upvotes: 2

Ali Shah Ahmed
Ali Shah Ahmed

Reputation: 3333

its simple:

\d{10}

\d allows digits, and {10} indicates that the phone number must be exactly 10 digits long.

As you mentioned in the comment that you also want a regex for 012-345-6789, (\d{3}-){2}\d{4} would do the work.

(\d{3}-) would validate the first 3 digits and a -

(\d{3}-){2} would look for two occurrence of the above described group. So will validate: 012-345-

And at last \d{4} will look for the last 4 digits

Upvotes: 2

Ravi Gadag
Ravi Gadag

Reputation: 15881

Use this pattern and match it:

\d{10}

\d : digits only.

{n}: numbers of occurrences.

You can refer this post for more info. Regex for Phone number

Upvotes: 2

Related Questions