Phone number validation with plus sign optional

别说谁变了你拦得住时间么 提交于 2021-02-04 21:56:13

问题


How can i validate a phone number with plus sign optional only at the beginning and after that any number of digits with number only.

I tried this:-

/^\+(?:[\d]*)$/

How will be the modification


回答1:


/^\+?(?:[\d]*)$/

The questionmark tells that the plus sign can be there or not. However, your expression can be optimized quite a bit:

/^\+?\d+$/ 

I changed the * to a + as the expression would match just a plus sign. \d* suggests that it should match 0 or more digits.

Here's a demo on Regexr.



来源:https://stackoverflow.com/questions/29555521/phone-number-validation-with-plus-sign-optional

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