3

I need a regular expression to match phone numbers. I just want to know if the number is probably a phone number and it could be any phone format, US or international. So I developed a strategy to determine if it matches.

I want it to accept the following characters: 0-9 as well as ,.()- and optionally start with a + (for international numbers). The string should not match if it has any other characters.

I tried this:

/\+?[0-9\/\.\(\)\-]/

But it matches phone numbers that have + in the middle of the number. And it matches numbers that contain alpha chars (I don't want that).

Lastly, I want to set the minimum length to 9 characters.

Any thoughts?

Thanks for any help, I'm obviously not too swift on RegEx stuff :)

Kobi
  • 135,331
  • 41
  • 252
  • 292
phil swenson
  • 8,564
  • 20
  • 74
  • 99
  • Unfortunately, only allowing numbers that will be a valid phone number is very troublesome when it comes to international. The length and style of international numbers varies greatly. It is however possible to say you want a 9 digit or greater number like he did below. – m4tt1mus Dec 09 '10 at 05:31
  • possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – mmmmmm Jan 17 '12 at 15:32

3 Answers3

4

Well, you're pretty close. Try this:

^\+?[0-9\/.()-]{9,}$

Without the start and end anchors you allow partial matching, so it can match +123 from the string :-)+123.

If you want a minimum of 9 digits, rather than any characters (so ---.../// isn't valid), you can use:

^\+?[\/.()-]*([0-9][\/.()-]*){9,}$

or, using a lookahead - before matching the string for [0-9/.()-]* the regex engine is looking for (\D*\d){9}, which is a of 9 digits, each digit possibly preceded by other characters (which we will validate later).

^\+?(?=(\D*\d){9})[0-9\/.()-]*$
Kobi
  • 135,331
  • 41
  • 252
  • 292
  • 1
    +1 This does answer the criteria stated in the question. The ITU standard that defines telephone numbering is probably worth mention: http://en.wikipedia.org/wiki/E.164 – msw Dec 09 '10 at 05:19
  • Note - You may have to escape the slashes using `\/` - I hallucinated the `.net` tag, so I didn't include it. Actually, I'll add it in just to be safe. – Kobi Dec 09 '10 at 05:31
  • This actually won't work because you didn't escape the () / or . properly. At least it won't work in the languages I have used. Although the syntax is correct. – m4tt1mus Dec 09 '10 at 05:41
  • @mattimus - What flavor are you testing this on? You usually don't have to escape them in character classes. Here's an example: http://rubular.com/r/TrNUkWUiyT Some languages also need `/` to be escaped, which I've edited into the answer 12 minutes ago `:)` – Kobi Dec 09 '10 at 05:44
  • I ended up with /^\+?[ \/.,()-]*([0-9][ \/.,()-]*){9,}$/ Thanks! – phil swenson Dec 11 '10 at 02:42
1

The reason why it matches alpha character is because of the period. You have to escape it. I don't know what editor you are using for this, this is what I'll use for VIM:

^+\?[()\-\.]\?\([0-9][\.()\-]\?\)\{3,\}$
nikeairj
  • 173
  • 10
  • I'm afraid that isn't true. You don't have to escape the dot in a character class (`[.]`). Also, you shouldn't escape `?`,`{` and `}` in your regex, but should escape the `+`. – Kobi Dec 09 '10 at 05:35
  • You are right about the dot escape, but I do have to escape ?, { and } for VIM. I don't know what you guys are using to write regex, so I'm just giving my knowledge based on VIM. – nikeairj Dec 09 '10 at 05:41
  • Very interesting variation - it seems opposite at first, but it does make sense. – Kobi Dec 09 '10 at 06:04
0

The juqeury has a plugin for US phone validation. Check this link. You can also see the regular expression in the source code.

lalit
  • 1,485
  • 2
  • 17
  • 32