我写了一个正则表达式来验证 WooCommerce 中的电话号码。它适用于所有情况,除非该字段包含单个“0”字符。
可能是什么错误?
那是代码:
$post_value = $_POST['billing_phone'];
if ( $post_value && ! preg_match( '/^(\+49)[0-9]{9,}$/', $post_value ) ) {
//error
}
您的正则表达式不匹配0
或+490
. 所以问题可能是当 POST 参数为 0 时,if 语句中的某些内容有所不同。'billing_phone'
在 PHPif (int)
中,对于 0 以外的任何 int 为 true,对于 0 为 false,也许这就是问题所在?
因此,您可以通过以下方式解决该问题:
if ( ($post_value || $post_value == 0) && ! preg_match( '/^(\+49)[0-9]{9,}$/', $post_value ) ) {
(免责声明:我一般对 PHP 不是很熟悉。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句