简体   繁体   中英

Javascript regular expression validate phone number

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

  • Start with (9|09|8869|+8869)
  • Followed by 8 digit [0-9]

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?

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/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM