如果该字段仅包含一个“0”,则 WooCommerce 电话号码字段验证不起作用特点
我编写了一个正则表达式来验证 WooCommerce 中的电话号码。它适用于所有情况,除非该字段包含单个“0”字符。
可能是什么错误?
这就是代码:
$post_value = $_POST['billing_phone'];
if ( $post_value && ! preg_match( '/^(\+49)[0-9]{9,}$/', $post_value ) ) {
//error
}
I wrote a regular expression that validates the phone number in WooCommerce. It works in all cases, unless the field contains a single "0" character.
What could be the error?
That's the code:
$post_value = $_POST['billing_phone'];
if ( $post_value && ! preg_match( '/^(\+49)[0-9]{9,}$/', $post_value ) ) {
//error
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的正则表达式与
0
或+490
不匹配。所以问题可能是当 POST 参数'billing_phone'
为 0 时,if 语句中的某些内容有所不同。在 PHP 中,if (int)
对于除0 和 false 为 0,也许这就是问题所在?所以你可以通过这样的方法来解决这个问题:(
免责声明:我通常对 PHP 不太熟悉。)
Your regex does not match
0
or+490
. So the problem is probably that something in that if statement is different when the POST param'billing_phone'
is 0. In PHPif (int)
is true for any int other than 0 and false for 0, perhaps that is the problem?So you could solve that by something like this:
(Disclaimer: I am generally not very familiar with PHP.)
您可以使用仅匹配单个零的交替来编写模式。
如果您不需要
+49
的单独捕获组进行后处理,则可以忽略它。请参阅正则表达式演示。
You could write the pattern using an alternation matching only a single zero.
If you don't need the separate capture group for the
+49
for after processing you can omit it.See a regex demo.