eHussain
eHussain

Reputation: 3367

Regular Expression for Phone Number

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.

Upvotes: 1

Views: 1312

Answers (1)

Jacob Relkin
Jacob Relkin

Reputation: 163238

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. :-)

Upvotes: 7

Related Questions