Search code examples
phpregexregular-language

Regular expression for phone number with special characters


I need a regular expression for phone numbers. The phone numbers may contain special characters like +, ., /, -, space, (, ), [, ].

Some Examples:

(+91) 9864081806
(+91)9864081806
(+91)(98640)81806
+91.98640.81806
[+91]09864081806
+91.986.408.1806
+91-986-408-1806
Maximum numbers = 15

The special characters can exists anywhere in the code. An opening bracket should have a closing one.

I have created this pattern but it does not work:

preg_match('/^[\[\(\0-9\s.\_\-\+\/\)\]]{3,15}$/', $phone_no)

Solution

  • Try with this:

    ^(?=(?:[^\d\n]*\d){1,15}[])]?$)(?:[+]?\d++|[(][+]?\d++[)]|[[][+]?\d++[]])(?:[ .-]?+(?:\d\d++|[(]\d\d++[)]|[[]\d\d++[]]))*+$

    You have a demo here.