I need to validate phone number in PHP. I have made following regex, but I also need to it to accept numbers starting with plus char, how can I do that?
^[0-9]$
The regex you provided in your question ( ^[0-9]$
) will only match a one digit phone number.
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+$
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
111-111-1111
(111)111-1111 x 1
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} $ /;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.