5

I am looking for regex for a telephone number but cant seem to find what i need.

Requirements

  • start with a plus sign (optional) eg. +442021234
  • allow brackets (optional) eg. (+44)2021234
  • allow any length of numbers
  • allow spaces between numbers eg. (+44) 202 1234
  • allow hyphens eg. (+44)-202-1234

Basically want to allow users to enter a telephone number with common special characters and will strip these away with JavaScript onBlur. Any help is much appreciated.

Kara
  • 6,115
  • 16
  • 50
  • 57
  • 2
    What have you tried so far? That's a long list of requirements (not including the stripping of the characters); do you have any code we can help debug or are you asking us to write it for you? – newfurniturey Nov 18 '13 at 18:17
  • 1
    Why not strip the characters first, and then just validate that you have 9 digits with Regex using `^\d{9}$`? That way, when you display the number, you can format it again however you'd like, so that it's consistent. – qJake Nov 18 '13 at 18:18
  • 2
    possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – Gergo Erdosi Nov 18 '13 at 18:40
  • 1
    Due to the way the website is setup, I am unable to strip the values before validation as this is triggered automatically. So i want to allow users to enter plus, hyphens, brackets and spaces and strip them away with JS after validation. The telephone number is split into 3 seperate input fields (country code, area code and phone number). It is a large system so i have many restrictions - one is that i have to use the same regex on all three boxes so i do not want to restrict the user in how many numbers they can enter. Thanks everyone for your help! –  Nov 19 '13 at 13:25

3 Answers3

9

You can try this regex:

/(?:\(?\+\d{2}\)?\s*)?\d+(?:[ -]*\d+)*$/
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This seems to cover most of what im looking for but i need it to not allow letters and ideally only a plus at the start of the number. Is this possible? –  Nov 19 '13 at 13:35
  • This won't allow letters and`+` is only optional at start. Didn't you need that? – anubhava Nov 19 '13 at 14:38
  • Thanks correct. I have got the point where need a seperate regex for a list of the special characters so the validation is not triggered when it executes. Regex with plus sign, hyphen and brackets. –  Nov 19 '13 at 14:58
  • So should I take out matching `+` and `( )` ? – anubhava Nov 19 '13 at 15:00
  • I guess to answer the original question - Yes. However, if you could also write a new regex which only allows '+', '-' and '()' - that would be great! –  Nov 19 '13 at 15:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41477/discussion-between-dvl-and-anubhava) –  Nov 19 '13 at 15:12
7

Consider the following Regex...

^\(?\+?[\d\(\-\s\)]+$

Good Luck!

gpmurthy
  • 2,397
  • 19
  • 21
1

Instead of matching the format, since you plan to strip the characters regardless, why not strip the characters first and then validate you have a number that's the length you require?

// strip all non-numeric values
var number = number.replace(/[^\d]/g, '');

// validate we have a number of a specific length
if (number.length == 9) {
    // valid number =]

}

If you have additional "number" rules, such as it has to start with 44, you could then use a regex after you've stripped the characters:

if (number.match(/44\d{7}/)) {
    // we have a (+44)* number

}
newfurniturey
  • 37,556
  • 9
  • 94
  • 102