-3

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)
  • You need to define your general rules for the matching as right now are too vague. For example: is this phone number valid `+91 12344 123 [[123]]`? As for your current rules It may be valid, however you probably don't want to allow such a thing... Please define all valid rules. Add extra examples for every rule. – Julio Apr 20 '19 at 11:17
  • (+91) 9864081806 (+91)9864081806 (+91)(98640)81806 +91.98640.81806 [+91]09864081806 +91.986.408.1806 +91-986-408-1806 Maximum numbers = 15 – suraj rawat Apr 22 '19 at 06:53
  • hi i add some example of rules i need – suraj rawat Apr 22 '19 at 06:56

1 Answers1

0

Try with this:

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

You have a demo here.

Julio
  • 5,208
  • 1
  • 13
  • 42