开发者

Enhance regex to validate phone number

I have created a regex for validating a telephone number with following requirement:

  • Allowed chars: + space ( ) – 0-9

  • + or ( can be first char after trim like (+61) 312 405 678 or +61 312 405 678.

  • Dash is allowed anywhere in the number.

  • Length min 8 max 16 – show error in case of boundary conditions

But I need to enhance it a bit. I want to validate that if + is in the number it must be only in the beginning but my regex is not checking this. Please help. This is my regex so far:

^开发者_高级运维[\\(?\\+?(\\d{2})\\)?[- ]?(\\d{0,})[- ]?(\\d{0,})[- ]?(\\d{0,})]{9,16}$


Adding \+{0,1} to the beginning should do the trick.

The finished regex would look like this

^\+{0,1}[\(?\+?(\d{2})\)?[ -]?(\d{0,})[- ]?(\d{0,})[- ]?(\d{0,})]{9,16}$


I would throw out all the optional characters, then check if it matches \\+?\\d{8,16}.

Alternatively, allow for arbitrary amounts of punctuation anywhere; [\\s().-]*(\\+[\\s().-]*)?(\\d[\\s().-]*){8,16}.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜