1

Help me write a regex for below conditions

  1. number can start with +
  2. number can contain - or . but not () and /
  3. number can start with 0
  4. Min number in the string should be 9 digits excluding extension details and starting +
  5. max number in the phone number field should not reach more than 14 excluding +
  6. if the string contains ex/ext/x then the digit after should not have more than 5 characters (normally 4)

this above should satisfy examples below

0-1234-123456
+91-1234-56789012
+91-1234-56789012 x1234
+91-1234-56789012 ex1234
+91-1234-56789012 ext12345
+91-1234-56789012x1234
+91-1234-56789012ex1234
+91-1234-56789012ext12345
91-1234-56789012
91-1234-56789012 x1234
91-1234-56789012 ex1234
91-1234-56789012 ext12345
91-1234-56789012x1234
91-1234-56789012ex1234
91-1234-56789012ext12345
91123456789012
91123456789012 x1234
91123456789012 ex1234
91123456789012 ext12345
91123456789012x1234
91123456789012ex1234
91123456789012ext12345
91.1234.56789012
91.1234.56789012 x1234
91.1234.56789012 ex1234
91.12345.6789012 ext12345
91.12345.6789012x1234
91.12345.6789012ex1234
91.12345.6789012ext12345
1-234-567-8901
1-234-567-8901 x1234
1-234-567-8901 ext1234
1 234 567-8901
1.234.567.8901
12345678901

I found few links online one of them is http://ericholmes.ca/php-phone-number-validation-revisited/

and on stackoverflow

A comprehensive regex for phone number validation

also

^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$

is not working for many of the above

Community
  • 1
  • 1
Yellow and Red
  • 695
  • 9
  • 31

2 Answers2

4
^\+?(\d[.\- ]*){9,14}(e?xt?\d{1,5})?$

Explanation;

  • ^ Asserts start of string
  • \+? Matches an optional plus
  • (\d[.\- ]*){9,14} between 9 and 14 single digits, possibly seperated by spaces, dots, or dashes.
  • (e?xt?\d{1,5})? Optionally a x possibly preceeded by an e or followed by a t. The letters always followed by between 1 and 5 numbers.
  • $ Asserts end of the string
Taemyr
  • 3,407
  • 16
  • 26
  • @YellowandRed Well, I cheated. I updated my answer after looking at GuyH's. - since I had originally written it in a rather stupdi manner. – Taemyr Nov 05 '14 at 14:31
  • The previous one you had updated didnot work for me but GuyH's worked out. Now its GuyH's not working. But I have to appreciate his effort. Thanks to you too. – Yellow and Red Nov 05 '14 at 14:33
  • @YellowandRed Stating how the regexps are failing makes it easier to do something about it. – Taemyr Nov 05 '14 at 14:34
  • did you vote down my question. I am sorry for any inconvinence. If you can vote up back that will keep me happy. – Yellow and Red Nov 05 '14 at 15:01
  • 1
    @Yellow and Red, I didn't downvote it, I was happy to answer it as it was. Anyone could have done it. They probably saw the inconsistencies (especially over the brackets), decided it was too mixed up to bother with. – GuyH Nov 05 '14 at 17:07
1

This will do it, but depending on which language you are programming in (we always need to know that with regexs, so if this doesn't work for you, reply with the language used. I've tested it in PHP5.)

Your condition 5 (max 14 chars in the phone no) appears to be in error, since several of your examples contain 16 characters if they include dots or hyphens. In any case, this does not check for overall length of the whole thing because of the other length checks it does; it would need a second regex, or, better, check the string length beforehand (eg in PHP by doing a call to strlen).

You might want to allow for a space in extension numbers, eg ext 1234; if so add \s* in the appropriate place.

I hope this helps.

^\+?\d[\d-\.\s]{8,15}\s?((ext|ex|x)\d{3,5})?$
GuyH
  • 786
  • 1
  • 4
  • 8
  • Rule 2 state that paratheses are illegal, and edit have removed them from the list. – Taemyr Nov 05 '14 at 13:30
  • condition 4 and 5 states limitations on number of digits, not number of characters. – Taemyr Nov 05 '14 at 13:32
  • Thank you. And thank you also, Taemyr, for alerting me to the fact that the round brackets have been removed from your examples, and that condition 2 stands as originally stated. So, Yellow and Red, you might like to remove the 4 characters where I said .....\\(\\)..... so it won't allow brackets. – GuyH Nov 05 '14 at 13:54
  • I've edited the answer now to remove the bit allowing round brackets – GuyH Nov 05 '14 at 14:01
  • this regex have problem !! test this number: `0--------` ,, http://regex101.com/r/bL5yG7/1 – Baba Nov 05 '14 at 14:02
  • the previous one worked properly. I think () is also ok. My client didnot want (), but I think I can convience him – Yellow and Red Nov 05 '14 at 14:15