wowsohigh
wowsohigh

Reputation: 11

Javascript regular expression validate phone number

I need to validate phone number by regular expression. The phone number should

I come up with /(09|9|8869|+8869)[0-9]{8}$/g. I test with +8869900000000 and expect it will not match but actually it passed Could you help me to address the regex problem? And how do I fix it?

Upvotes: 0

Views: 1526

Answers (2)

Alex G
Alex G

Reputation: 1917

You can use this regex: /^(0?9|\+?8869)\d{8}$/

The group (0?9|+?8869) is for your starting condition where 0 is optional before 9 and + is optional before 8869.

Demo: https://regex101.com/r/1OpYl0/1/

Upvotes: 1

Pratham
Pratham

Reputation: 1713

The regex you are looking for is : ^(([0]?9)|([+]?8869))[0-9]{8}$

Note the way round brackets used to determine the conditions. We need to match within any of the 2 subsets and then precede it 8 digits.

Upvotes: 0

Related Questions