最小电话号码位数Woocommerce结帐 [英] Minimum Phone numbers digits Woocommerce Checkout

查看:83
本文介绍了最小电话号码位数Woocommerce结帐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户在Woocommerce结帐中至少输入7个电话号码.下面的函数可以使用,但是它包含空格,因此,如果用户输入6个数字和1个空格,它将被视为7.如何更改该函数,使其仅计算数字而不是空格,因此用户必须输入至少7个数字

I want the user to enter a minimum of 7 numbers for their phone number in the Woocommerce checkout. The function below works but it includes spaces, so if user enter 6 numbers with 1 space it counts it as 7. How Can I change the function so that it only counts numbers not spaces so that the user has to enter a minimum of 7 numbers.

// validation for Billing Phone checkout field
add_action('woocommerce_checkout_process', 'custom_validate_billing_phone');
function custom_validate_billing_phone() {
    $is_correct = preg_match('/^[0-9 \-]{7}/i', $_POST['billing_phone']);
    if ( $_POST['billing_phone'] && !$is_correct) {
        wc_add_notice( __( 'Phone Number must be <strong>minimum 7 numbers</strong>.' ), 'error' );
    }
}

推荐答案

我想出了解决方案.

$is_correct = preg_match('/^[0-9\D]{7}/i', $_POST['billing_phone']);

这篇关于最小电话号码位数Woocommerce结帐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆