Java 电话号码验证正则表达式在开头和前面的数字中包含一个加号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21049557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:11:51  来源:igfitidea点击:

Phone number validation regular expression consist one plus sign in start and preceding digits

javascriptjavaregexasp.net-mvc-4knockout.js

提问by Sami

I am trying to validate phone number but cannot.

我正在尝试验证电话号码,但不能。

My requirement is phone number consist only of digits and + (plus symbol). The + can be only the first character.

我的要求是电话号码仅由数字和 +(加号)组成。+ 只能是第一个字符。

For example: +123456489

例如:+123456489

I am using this regular expression but it is not working:

我正在使用这个正则表达式,但它不起作用:

/^\+(?:[0-9]??)$/

Thanks in advance.

提前致谢。

回答by brandonscript

I'd use this instead:

我会用这个代替:

^\+?\d*$

Matches your + at the start, then any digit, dash, space, dot, or brackets.

匹配开头的 +,然后匹配任何数字、破折号、空格、点或括号。

See it in action: http://regex101.com/r/mS9gD7

看到它在行动:http: //regex101.com/r/mS9gD7

回答by Srb1313711

\+[0-9]+

Try the above regex. You can test here http://gskinner.com/RegExr/. Are you also looking to validate length?

试试上面的正则表达式。你可以在这里测试http://gskinner.com/RegExr/。您是否还想验证长度?

回答by Henry Tshobo

You can try this ^+[1-9]{1}[0-9]{10}$

你可以试试这个^+[1-9]{1}[0-9]{10}$

回答by tk_

How about this?

这个怎么样?

^[\+\d]?(?:[\d-.\s()]*)$

works fine with bellow test cases:

适用于以下测试用例:

test-string

测试字符串

You can test it here : https://regex101.com/r/mS9gD7/39

你可以在这里测试:https: //regex101.com/r/mS9gD7/39

回答by Rahul Gupta

If you only want to allow +sign and that is only at the beginning of the number and does not want to allow any other characters or symbols other that the digits, then try the below regex:

如果您只想允许+符号并且仅在数字的开头并且不想允许数字以外的任何其他字符或符号,请尝试以下正则表达式:

var regEx = /^[+]?\d+$/;
regEx.test("+123345"); // this will be true
regEx.test("++123345"); // this will be false
regEx.test("1+23345"); // this will be false
regEx.test("111222"); // this will be true

回答by sarfaraz akhtar ansari

^[0-9]+[0-9]*$ try this its working

^[0-9]+[0-9]*$ 试试这个它的工作原理