Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
169 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...