regex optionally allowed parentheses, dots, dashes, spaces, and plus sign in phone number

眉间皱痕 提交于 2019-12-11 05:08:42

问题


I'm trying to validate the phone number value, I have this regEx /^\d+(-\d+)*$/ allowing a number with dashes in between of numbers. I find it here, but I forgot the link. And I try to edit it to optionally allowed parentheses, dots, dashes, spaces, and plus sign but I'm a beginner so that for now I can't do this to work. And please note that the user is free what format they are like to use, it simply allow them to input either dashes, space, plus sign, dots, and parentheses.

Value allowed:

  • 0123456789
  • 012-345-6789
  • 012-345-6789
  • (012)-345-6789
  • (012) 345-6789
  • +0123456789
  • +012-345-6789
  • +(012)-345-6789

Someone please help me.


回答1:


[0-9\-\(\)\+\s]+

You can use that to check and/or use a regex replace to remove all those characters and validate the result.

For replacement, you might want: [^0-9]



来源:https://stackoverflow.com/questions/13677058/regex-optionally-allowed-parentheses-dots-dashes-spaces-and-plus-sign-in-pho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!