最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript regex : for US phone number - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Aug 29, 2015 at 5:15 Samir Shah asked Aug 28, 2015 at 1:42 Samir ShahSamir Shah 7093 gold badges12 silver badges23 bronze badges 11
  • can you show the format you are trying to match? – Ibu Commented Aug 28, 2015 at 1:43
  • something like : (000) 234 - 5678 should be invalid. – Samir Shah Commented Aug 28, 2015 at 1:45
  • (367) 123 - 4567 should be invalid. as 4th digit is 1, which is not allowed. – Samir Shah Commented Aug 28, 2015 at 1:45
  • Quantifiers like {m,n} go after the expression they're quantifying. – Mark Reed Commented Aug 28, 2015 at 2:00
  • possible duplicate of A prehensive regex for phone number validation – ThisClark Commented Aug 28, 2015 at 2:07
 |  Show 6 more ments

4 Answers 4

Reset to default 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}).

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}$
/^\D([2-9])(?!\1\1)\d{2}\D\s+[2-9]\d{2}\s+\W\s+\d{4}$/

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" />

发布评论

评论列表(0)

  1. 暂无评论