[英]Phone number validation in PHP
I need to validate phone number in PHP. 我需要在PHP中验证电话号码。 I have made following regex, but I also need to it to accept numbers starting with plus char, how can I do that?
我做了以下正则表达式,但是我还需要它来接受以plus char开头的数字,我该怎么做?
^[0-9]$
The regex you provided in your question ( ^[0-9]$
) will only match a one digit phone number. 您在问题中提供的正则表达式(
^[0-9]$
)仅与一位数字的电话号码匹配。
At the very least you probably need to add the +
modifier to allow one or more digits: 至少您可能需要添加
+
修饰符以允许一个或多个数字:
^[0-9]+$
To add an optional +
at the beginning, you'll need to escape it since +
is a special character in regular expressions: 要在开头添加可选的
+
,您需要对其进行转义,因为+
是正则表达式中的特殊字符:
^\+?[0-9]+$
And finally, \\d
is shorthand for [0-9]
so you could shorten the expression to 最后,
\\d
是[0-9]
简写,因此您可以将表达式缩短为
^\+?\d+$
Try this: 尝试这个:
/^[\+]?([0-9]*)\s*\(?\s*([0-9]{3})?\s*\)?[\s\-\.]*([0-9]{3})[\s\-\.]*([0-9]{4})[a-zA-Z\s\,\.]*[x\#]*[a-zA-Z\.\s]*([\d]*)/
will match: 将匹配:
+1 ( 111)111-1111, #111 +1(111)111-1111,#111
111-111-1111 111-111-1111
(111)111-1111 x 1 (111)111-1111 x 1
111.111.1111x1 111.111.1111x1
etc. 等等
使用此/ ^(+ \\ d) \\ s ((\\ d {3})\\ s *)* \\ d {3}(-{0,1} | \\ s {0,1})\\ d {2} (-{0,1} | \\ s {0,1})\\ d {2} $ /;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.