Samir Shah
Samir Shah

Reputation: 709

javascript regex : for US phone number

Hi I am trying to write regex but facing some issues over it. can anyone help me write one.

conditions :

• ALL 10 digits being 0 is not allowed.
• The area code (first 3 digits) cannot be the same digit,
• The 1st and 4th digit cannot be 0 or 1.

/^\({0,1}[2-9]{1}[0-9]{2}\){1} {1}[2-9]{1}[0-9]{2}-{0,1}[0-9]{0,4}$/

Example format: (234) 567-7890

The above question is different than the other ones as it focuses more on a specific conditions to fulfill with regex.

Upvotes: 3

Views: 9022

Answers (4)

Pedro Affonso
Pedro Affonso

Reputation: 1676

Let's go by parts, you got three conditions:

  1. ALL 9 digits being 0 is not allowed
  2. The area code (first 3 digits) cannot be the same digit,
  3. The 1st and 4th digit cannot be 0 or 1.

Condition 1 is redundant if you consider condition 3; A simple regex not considering condition 2 is:

/^\([2-9]\d\d\) [2-9]\d\d-\d{4}$/

Assuming you want parenthesis and spaces - (555) 555-5555

Explanation:

  • \d matches any digit
  • [2-9] matches any character from 2 to 9
  • space and dash are literals - match spaces and dash
  • {4} is a quantifier - will match 4 digits in this case
  • ( and ) are escaped literals - will match ( and ) respectively

Now if we want to consider condition number 2 in our expression, we use

  • a negative lookahead ?!
  • a capturing group () and
  • a back reference \1.

Read some regex reference if you want to fully understand those. The full expression is:

^\(([2-9])(?!\1\1)\d\d\) [2-9]\d\d-\d{4}$

Upvotes: 2

guest271314
guest271314

Reputation: 1

Try setting input attribute maxlength to 10 , utilizing Array.prototype.map , Array.prototype.every

/*
• ALL 9 digits being 0 is not allowed.
• The area code (first 3 digits) cannot be the same digit,
• The 1st and 4th digit cannot be 0 or 1.
*/
document.querySelector("input").oninput = function(e) {
  var vals = this.value.split("");
  if (vals.length === 10) {
    var all = vals.map(Number).every(function(n) {
      return n === 0
    })
    , area = vals.map(Number).slice(1, 3).every(function(n) {
      return n === Number(vals[0]);
    })
    , numbers = (Number(vals[0]) === (0 || 1)) || (Number(vals[3]) === (0 || 1))
    , res = all === area === numbers;
    if (!!res) {
      alert("invalid entry")
    } else {
      alert(vals.join(""))
    }
  }
}
<input type="text" maxlength="10" />

Upvotes: 0

Mark Reed
Mark Reed

Reputation: 95242

So, first, I should point out that requiring US-formatted telephone numbers is pretty restrictive; international numbers can have very different rules. That said, this regex should meet your needs:

/(?:^|\D)\(([2-9])(?:\d(?!\1)\d|(?!\1)\d\d)\)\s*[2-9]\d{2}-\d{4}/

First,to prevent matching things that end with a valid phone number but have extra junk up front, we match either the start of the string (^) or a non-digit (\D). Then the opening parenthesis of the area code, (\().

Then we match the first digit of the area code, [2-9].

Then we match either any digit (\d) followed by any digit except the first one ((?!\1)\d), or the other way around ((?!\1)\d\d). This keeps the area code from being three identical digits.

Then we close the parens (\)), allow (but don't require) space (\s*) before the first digit of the prefix ([2-9] again), followed by any two digits (\d{2}), a hyphen, and any four digits (\d{4}).

Upvotes: 4

Vidul
Vidul

Reputation: 10528

/^\D([2-9])(?!\1\1)\d{2}\D\s+[2-9]\d{2}\s+\W\s+\d{4}$/

Upvotes: 0

Related Questions