首页 > 解决方案 > Problem validating a phone number beginning with 256 using Javascript

问题描述

I am trying to validate a phone number beginning with 256 and the whole phone number should be a maximum of 12 characters, an example of the phone number is 256732111890 I am using the following regex which seems not to work

^(?:[256]●?){6,14}[0-9]$

标签: javascriptregex

解决方案


我将发布模式,在这里解释。

图案:^256\d{9}$

在哪里

  • ^= 断言行首
  • 256= 匹配字符串 256
  • \d{9}= 在找到“256”后匹配任意数字 9 次(精确)。
  • $= 该行必须在找到 9 个数字后结束。

此外,一个很好的选择是^256\d{7,9}$@Pushpesh Kumar Rajwanshi 发布的谁与我的模式不同:

  • \d{7,9}: 在找到 '256' 后匹配 7 到 9 次之间的任何数字。

推荐阅读