1

I try to write a regex that will check validity of phone number inputs. There are several rules:

  1. First character should be either '+' or '(' or digit
  2. Can have unlimited number of space characters
  3. Can have 8-11 digits total

I managed to come up with this:

^(\+|\()?(\)?|\ *|\d{8,11})$

The problem is that this only matches 8-11 digits in a row, but the digits can be anywhere, only their total count should be 8-11.

Granga
  • 1,612
  • 1
  • 14
  • 20
  • Is a plus withing brackets possible? And furthermore I assume that you can put digits within the brackets? – Willem Van Onsem Feb 18 '15 at 16:49
  • Run it in a "cleanString()" first (removing anything but digits) and then check the length. No need for regex here. – Nir Alfasi Feb 18 '15 at 16:49
  • @alfasin: that's not really the problem, by using `( *\d){8,11} *`, you ignore all the whitespace in between. – Willem Van Onsem Feb 18 '15 at 16:57
  • @CommuSoft what about other characters? a few I can think of are `[+-.]` – Nir Alfasi Feb 18 '15 at 16:58
  • Simply add them to the regex as well: `([+-. ]*\d){8,11}[+-. ]*` That means you can put an unconstrained amount of these characters between two numbers, and optionally (if present in the regex) at the end as well. I think the main problem is that Granga needs to be more specific on the exact format and that the current format does not fully match telephone formats. – Willem Van Onsem Feb 18 '15 at 16:59

2 Answers2

2

I assume you want to implement this format but without dashes? Furthermore there are more strict rules if you want to parse American telephone numbers. Please update your question and be more specific.

This regex can be used to match all 8-11 digits with a plus in front.

^\+?( *\d){8,11} *$

The interesting part is the ( *\d){8,11} *. Each group between the brackets matches an unlimited number of spaces (can be zero, followed by a digit). So in total you have 8 to 11 digits and the spaces in between are unconstrained. You also need to put * at the end to handle tailing spaces.

Now the problem is more complicated if you want to allow brackets, because the brackets also take some numbers. In case the total amount of digits is fixed to three), you can write it as

^\(( *\d){3} *\)( *\d){5,8} *$

Now you can generalize this method and produce:

^\(( *\d){1} *\)( *\d){7,10} *$
^\(( *\d){2} *\)( *\d){6,9} *$
^\(( *\d){3} *\)( *\d){5,8} *$
^\(( *\d){4} *\)( *\d){4,7} *$
^\(( *\d){5} *\)( *\d){3,6} *$
^\(( *\d){6} *\)( *\d){2,5} *$
^\(( *\d){7} *\)( *\d){1,4} *$
^\(( *\d){8} *\)( *\d){0,3} *$
^\(( *\d){9} *\)( *\d){0,2} *$
^\(( *\d){10} *\)( *\d){0,1} *$
^\(( *\d){11} *\) *$

And now it's only a matter of combining them:

^(\+?( *\d){8,11} *)|
 \(( *\d){1} *\)( *\d){7,10} *|
 \(( *\d){2} *\)( *\d){6,9} *|
 \(( *\d){3} *\)( *\d){5,8} *|
 \(( *\d){4} *\)( *\d){4,7} *|
 \(( *\d){5} *\)( *\d){3,6} *|
 \(( *\d){6} *\)( *\d){2,5} *|
 \(( *\d){7} *\)( *\d){1,4} *|
 \(( *\d){8} *\)( *\d){0,3} *|
 \(( *\d){9} *\)( *\d){0,2} *|
 \(( *\d){10} *\)( *\d){0,1} *|
 \(( *\d){11} *\) *$

But I think @DaveKirby makes a valid point here. The rules differ that much between different regions and in time (who says we will write phone numbers the same way within 20 years?) that you better don't aim to capture them.

Community
  • 1
  • 1
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Thak you, this is very helpful. Let me remind you that there isn't rule how many digits should be inside brackets. Not sure if it is good practice, but I combined your rule few more times and ended up with this: https://regex101.com/r/eQ3vI4/1 – Granga Feb 18 '15 at 17:25
  • @Granga: does it affect the number of digits **after** the brackets? – Willem Van Onsem Feb 18 '15 at 17:26
  • Nope, just the total number of digits anywhere in the string should be between 8 and 11. – Granga Feb 18 '15 at 17:35
  • Well in that case it affects the number of digits after brackets: if the sum of two numbers is fixed, the two numbers are related... – Willem Van Onsem Feb 18 '15 at 17:35
  • Not sure I understand what you mean. My solution (with your help) will now check all the rules mentioned in the question, with one addition that there can be 1-3 digits inside the brackets, the rest of the digits will be somewhere after the brackets. – Granga Feb 18 '15 at 17:42
  • The answers of this kind of questions will always be debatable. If it was up to me only, I would have gone with @DaveKirby's solution. – Granga Feb 18 '15 at 17:44
0

Duplicate of A comprehensive regex for phone number validation

This question is pretty complicated when it comes to supporting international phone numbers, because rules may vary a lot from one country to another, or in some (big) countries like Brazil the rules may vary from mobile to landline for example.

Community
  • 1
  • 1
lnrdo
  • 396
  • 1
  • 13