3

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.

Samir Shah
  • 709
  • 3
  • 12
  • 23
  • can you show the format you are trying to match? – Ibu Aug 28 '15 at 01:43
  • something like : (000) 234 - 5678 should be invalid. – Samir Shah Aug 28 '15 at 01:45
  • (367) 123 - 4567 should be invalid. as 4th digit is 1, which is not allowed. – Samir Shah Aug 28 '15 at 01:45
  • Quantifiers like `{m,n}` go *after* the expression they're quantifying. – Mark Reed Aug 28 '15 at 02:00
  • possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – ThisClark Aug 28 '15 at 02:07
  • and [Validate phone number with JavaScript](http://stackoverflow.com/questions/4338267/validate-phone-number-with-javascript) – ThisClark Aug 28 '15 at 02:07
  • says `ALL 9 digits being 0 is not allowed.` then shows an example - `(367) 123 - 4567` showing 10 digits – Jaromanda X Aug 28 '15 at 02:14
  • @JaromandaX `'(367) 123 - 4567'.indexOf('0') // -1` Where did you see `0` in this string? – Vidul Aug 28 '15 at 02:28
  • @Vidul - there are **10** digits in the example number, but _all 9 being 0 is not allowed_ the inconsistency has nothing to do with the digit 0, it has to do with counting up to 10 – Jaromanda X Aug 28 '15 at 02:33
  • @JaromandaX my guess is OP just had a brainfart and meant 10, seeing as how a valid U.S. number has 7 digits for the local number + 3 digits for the area code for a total of 10 digits – CrayonViolent Aug 28 '15 at 02:35
  • I think Mark Reed wrote exactly what you need. Have a look. – SergeyAn Aug 28 '15 at 03:58

4 Answers4

4

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

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
2

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}$
Pedro Affonso
  • 1,656
  • 15
  • 26
0
/^\D([2-9])(?!\1\1)\d{2}\D\s+[2-9]\d{2}\s+\W\s+\d{4}$/
Vidul
  • 10,128
  • 2
  • 18
  • 20
0

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" />
guest271314
  • 1
  • 15
  • 104
  • 177
  • the logic is correct but I was more focusing on regex but I can definitely try to implement that if it works out. Thanks for your efforts. – Samir Shah Aug 29 '15 at 05:17