1

I need a Javascript RegEx through which I can validate phone number. RegEx should handle following criteria

  1. It should only consist of numbers ( ) + and -
  2. Count of + should not exceed 1
  3. Count of - should not exceed 4
  4. There must be only one pair of ()
  5. If '(' is present in phone number then ')' must be present.

Thanks for the help!

Hussain.

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
eHussain
  • 3,297
  • 1
  • 21
  • 19
  • 2
    I guess your number format is really "+ (1234) 5678-1234..." should the regex match that, or allow ")234----1(+", as currently supported by your specs? – Jens Feb 16 '11 at 07:49
  • 2
    Rather than just requesting a complete expression, have you tried anything yet? [Here is a library of them](http://regexlib.com/DisplayPatterns.aspx?cattabindex=6&categoryId=7). – slugster Feb 16 '11 at 07:53
  • Why dont you try the jQuery Input Mask Plugin for the same ? http://digitalbush.com/projects/masked-input-plugin/ – Clyde Lobo Feb 16 '11 at 08:51
  • @Jens, yes ")234----1(+" should not be allowed. @slugster I can't use any library just because these library won't match my specifications. – eHussain Feb 16 '11 at 09:54
  • 1
    If Jacob's solution does not help you, you should update your question to clarify your specs, and ive examples of both mathing and not matching numbers. – Jens Feb 16 '11 at 09:57

1 Answers1

7

Try this:

function valid_phone_number(ph) {
  var regex = /^(?!([^-]*-){5})(\+\d+)?\s*(\(\d+\))?[- \d]+$/gi;
  return regex.test(ph);
}

I'm new to regular expressions, so please be nice. :-)

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • 1
    This regex has a few problems. I'll try to be nice =) The term `{1}` is redundant. It does not allow a space between + and the first number and it seems to restrict the length of number blocks to 3 or 4. – Jens Feb 16 '11 at 08:21
  • @Jacob: Sorry, the enter key caught me by surprise. On second thought, I think maybe a space after the plus is unnesseccary... =) – Jens Feb 16 '11 at 08:24
  • @Jens: Thank you for your input. Please see my revised solution. – Jacob Relkin Feb 16 '11 at 08:25
  • @Jacob: Hmm.. I should read more carefully, sorry. Those dashes are all optional.... I'll look at it some more! – Jens Feb 16 '11 at 08:26
  • @Jens: I've *just* started learning regexps last week, all this stuff is brand new to me. I've been programming for nearly 5 years and I'm just starting to see a use for them, so here I am! :) – Jacob Relkin Feb 16 '11 at 08:28
  • 1
    In addition to Jens' comments, `{1,}` can simply written as `+`. A general rule on quantifiers: `*` means zero or more, `+` means one or more, `?` means zero or one. Only use `{}` if you need other quantities. – vonconrad Feb 16 '11 at 08:48
  • 2
    Ok, more thoughts. =) Country and city part look good now, although I'd use `\d` instead of `[0-9]`, `+` instead of `{1,}` and `\s*` instead of `\s*?`. The rest currently only allows for two dashes, and adding more would make the regex still longer. Without the four dashes requirement I'd just use `[- \d]+` to allow any combination of dash, space and digits. Then, one could use `(?!([^-]*-){5})` at the start to disallow five or more dashes. – Jens Feb 16 '11 at 08:48
  • @vonconrad Thank you! @Jens I'm not sure I understand the `[- \d]+` part, could you explain that one in a bit more detail? Thank you! – Jacob Relkin Feb 16 '11 at 09:00
  • @Jacob `[]` denotes a character range, meaning that any character within that range is accepted. `[- \d]` means that the only accepted characters are digits (`\d`), dashes and spaces. – vonconrad Feb 16 '11 at 09:03
  • @vonconrad Ah okay I see. Wow that'd make this regex so much cleaner! I'm going to try it! Thank you all! You're really making regexes so **exciting**! – Jacob Relkin Feb 16 '11 at 09:06
  • @vonconrad @Jens Please see my updated answer based on your great suggestions. I really appreciate your help - this is something I've wanted to learn for a really long time and I'm finally learning it! xD – Jacob Relkin Feb 16 '11 at 09:14
  • You probably don't need the `/i` flag--numbers, whitespace and the few symbols you have in the regex don't have a case. :) – vonconrad Feb 16 '11 at 09:53
  • While this regex is for his requirements - it does not correctly handle international numbers or alternative formatting. Phone number validation should be done but _stripping_ extra characters and doing validation on that. – Good Person Feb 16 '11 at 22:42