如何将此PHP正则表达式更新为不允许超过10位数的电话号码?(How to update this PHP regex to not allow more than 10 digits in a phone number?)
我正在使用以下正则表达式来确保输入有效的电话号码。 如果输入的数字超过10位,它目前不会标记错误。
怎么解决这个问题?
public function phoneNumber($value) {
if (preg_match("/^\(?[0-9]{3}\)? *-? *[0-9]{3} *-? *[0-9]{4}$/", $value)) {
return true;
}
return false;
}
这是一个当前显示为有效电话号码的测试用例,不应该:
35353535355535555555
I'm using the following regex to ensure a valid phone number is entered. It currently does not flag an error if more than 10 digits are entered.
How can this be fixed?
public function phoneNumber($value) {
if (preg_match("/^\(?[0-9]{3}\)? *-? *[0-9]{3} *-? *[0-9]{4}$/", $value)) {
return true;
}
return false;
}
Here's a test case that is currently showing up as a valid phone number, when it shouldn't:
35353535355535555555
原文:https://stackoverflow.com/questions/15650859
更新时间:2019-12-28 00:08
最满意答案
如果您只对数字感兴趣,可以过滤掉其他所有内容并检查结果:
public function phoneNumber($value) {
$filtered = preg_replace("/\D/","",$value);
return strlen($filtered) == 10;
}
If you're only interested in the numbers, you can filter out everything else and check the result:
public function phoneNumber($value) {
$filtered = preg_replace("/\D/","",$value);
return strlen($filtered) == 10;
}
2013-03-27
相关问答
^\D*(?:\d\D*){10,}$
^^
[+()-]
只是指出你的正则表达式问题 \D :除数字之外的任何字符 ^\D*(?:\d\D*){10,}$
^^
[+()-]
just point out your regex problem \D: any characters except digits
当我看到你的字符串时,你的数字之间有一个空格,如果你想严格做出选择,这就是正则表达式: [62]\d{2}\s*\d{7}
说明 : [62] # Start with 6 or 2
\d{2} # 2 more digits
\s* # any number of white spaces
\d{7} # 7 more digits
现场演示 和PHP代码,它具有preg_match_all以匹配所有这些字符串: preg_match_all("/[62]\d{2}\s
...
^(?!(\d)\1{9})(?!0123456789|1234567890|0987654321|9876543210)\d{10}$ 你可以使用类似上面的模式。 您需要拼出序列,因为正则表达式中没有内置任何内容来检查序列TMK。 检查全零或所有“单个数字”可以通过相同的负lookeahead完成。 在该前瞻中,您可以使用捕获组来简化模式。 ^(?!(\d)\1{9})(?!0123456789|1234567890|0987654321|9876543210)\d{10}$ You can u
...
以下正则表达式有效 ^\(\d{3}\) \d{3}-\d{4}$
^ = start of line
\( = matches parenthesis open
\d = digit (0-9)
\) = matches parenthesis close
The following regex works ^\(\d{3}\) \d{3}-\d{4}$
^ = start of line
\( = matches parenthesis open
\d = digit (0-9)
\)
...
匹配三个部分中的每一个,用短划线分隔,如下所示: ^0\d-\d{4}-\d{4}$
这匹配0x-xxxx-xxxx。 演示: https : //regex101.com/r/nW7wL5/1 如果您还想匹配没有短划线的数字,请使用 ^0\d-?\d{4}-?\d{4}$
演示: https : //regex101.com/r/gY0mC3/1 \d与[0-9]相同,但它更短。 Match every of the three parts, separated by the dashes,
...
你很亲密! 这是你正在寻找的正则表达式: ^(\d{10}|\d{12})$ 。 它检查数字(用\d )。 其余的或多或少都是你的代码,但括号除外。 它捕获每个组。 如果你想在没有它的情况下工作,你可以放松它们! 在这里看到它 You're close! This is the regex you're looking for: ^(\d{10}|\d{12})$. It checks for digits (with \d). The rest is more or less your code
...
如果您只对数字感兴趣,可以过滤掉其他所有内容并检查结果: public function phoneNumber($value) {
$filtered = preg_replace("/\D/","",$value);
return strlen($filtered) == 10;
}
If you're only interested in the numbers, you can filter out everything else and check the result
...
^\+?(\d[.\- ]*){9,14}(e?xt?\d{1,5})?$
说明; ^断言字符串的开头 \+? 匹配可选加号 (\d[.\- ]*){9,14}介于9到14个单位数之间,可能用空格,圆点或短划线分隔。 (e?xt?\d{1,5})? 可选地,ax可能先于e或后跟t。 这些字母后面跟着1到5个数字。 $ Asserts字符串的结尾 ^\+?(\d[.\- ]*){9,14}(e?xt?\d{1,5})?$
Explanation; ^ Asserts start of strin
...
看起来你可以使用负面lookahead内的交替操作符来检查2个条件: ^(?!(\d)\1+$|\d*(\d)\2{6}$)(?:\d{7,15})?$
查看正则表达式演示 。 细节 : ^ - 字符串的开头 (?!(\d)\1+$|\d*(\d)\2{6}$) - 如果所有数字从开始到结束都是相同的(否则(\d)\1+$其中(\d)将数字捕获到组1中,然后\1+与组1中捕获的一个或多个值匹配,然后使用$ )进行字符串检查,或者只有最后7个相同(请参阅\d*(\d)\2{6}$其中\d*匹配0+
...
在我看来你想要一个可选的前导“1”后跟“800”后跟7个字母或7个数字,所以以下内容应该适合: var re = /^1?800[a-z]{7}$|^1?800[0-9]{7}$/i;
alert(re.test('800yousave')); // true
alert(re.test('1800yousave')); // true
alert(re.test('1800yousavee')); // false
alert(re.test('8001234
...