如果该字段仅包含一个“0”,则 WooCommerce 电话号码字段验证不起作用特点

发布于 2025-01-15 16:43:53 字数 344 浏览 5 评论 0原文

我编写了一个正则表达式来验证 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯沦落人 2025-01-22 16:43:53

您的正则表达式与 0+490 不匹配。所以问题可能是当 POST 参数 'billing_phone' 为 0 时,if 语句中的某些内容有所不同。在 PHP 中,if (int) 对于除0 和 false 为 0,也许这就是问题所在?

所以你可以通过这样的方法来解决这个问题:(

if ( ($post_value || $post_value == 0) && ! preg_match( '/^(\+49)[0-9]{9,}$/', $post_value ) ) {

免责声明:我通常对 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 PHP if (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:

if ( ($post_value || $post_value == 0) && ! preg_match( '/^(\+49)[0-9]{9,}$/', $post_value ) ) {

(Disclaimer: I am generally not very familiar with PHP.)

药祭#氼 2025-01-22 16:43:53

您可以使用仅匹配单个零的交替来编写模式。

如果您不需要 +49 的单独捕获组进行后处理,则可以忽略它。

^(?:\+49\d{9,}|0)$

请参阅正则表达式演示

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.

^(?:\+49\d{9,}|0)$

See a regex demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文