Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I proper validate the phone number field?

I'm stuck at a point. I need to validate a phone number by preventing below formats. For now I've applied the 10 digits validation but below formats are not correct.

0000000000
1111111111
2222222222
3333333333
4444444444
5555555555
6666666666
7777777777
8888888888
9999999999

also I need to avoid those numbers which are starting with either 1 or 0. Like validation should avoid below formats as well.

1xxxxxxxxx
0xxxxxxxxx

Can anyone suggest me a way of doing that ?

like image 267
Vineet Avatar asked Feb 09 '23 10:02

Vineet


1 Answers

You may try this.

^(?!(\d)\1{9}$)[2-9]\d{9}$
  • (?!(.)\1{9}$) won't be the same digit which repeats exactly 10 times.
  • [2-9] must starts with 2 or 3 or upto 9.
  • \d{9} any 9 digits.
  • $ line end.

DEMO

or

^([2-9])(?!\1{9}$)\d{9}$

DEMO

like image 84
Avinash Raj Avatar answered Feb 11 '23 22:02

Avinash Raj