1

Hi I need to build a regexp for phone numbers, such as:

+79261234567
+7 926 123 45 67
89261234567
79261234567
8(926)123-45-67
9261234567
79261234567
89261234567
8-926-123-45-67
8 927 1234 234
8 927 12 12 888
8 927 12 555 12
8 927 123 8 123

I came now to this regexp:

/((8|\+7)[\- ]?)((\(?9\d{2}\)?[\- ]?)[\d\- ]{7,10})?[\d\- ]{10,10}/g

link to regexper.com

But it's not correctly working.So any help would be appreciated

Dalorzo
  • 19,834
  • 7
  • 55
  • 102
user2950593
  • 9,233
  • 15
  • 67
  • 131
  • Is there any format for these numbers? or just 11 numbers with posible (, - and whitespace? – Justinas May 28 '14 at 13:52
  • There are lots of similar questions here. Have none of those answers helped? – Pointy May 28 '14 at 13:54
  • There's a solution provided by the community of Regexr: http://www.regexr.com/38pvb – Slate May 28 '14 at 13:56
  • You should have a look at [this question](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) which deals with this issue and is pretty clearly answered – user3241019 May 28 '14 at 13:57

1 Answers1

3

You can try with:

var input  = 'phone number',
    output = input.replace(/\(([^)]+)\)/, '$1').replace(/(^\+)|[- ]+/g, ''),
    result = /^\d{10,11}$/.test(output);

Explanation:

\(([^)]+)\) - looks for digits with brackets around them to be removed
(^\+)|[- ]+ - looks for starting `+` or whitespace/dash in number to be removed
^\d{10,11}$ - checks if number has exacly 10 to 11 digits
hsz
  • 148,279
  • 62
  • 259
  • 315