admin管理员组文章数量:1310455
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
4 Answers
Reset to default 4So, 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:
- 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.
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" />
本文标签: javascript regexfor US phone numberStack Overflow
版权声明:本文标题:javascript regex : for US phone number - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741824665a2399572.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论