我在woocommerce结帐字段中添加了用于验证移动号码的代码,regex也很好,但这里没有什么问题1)当客户输入错误的移动电话号码时,wp_add_notice没有向客户打印错误消息,当客户输入正确的移动号码时,就会显示错误消息,然后客户转到进一步的支付选项汇总:它不会打印我想要的错误移动号码的错误消息。
// Custom validation for Billing Phone checkout field
add_action('woocommerce_checkout_process', 'custom_validate_billing_phone');
function custom_validate_billing_phone() {
$is_correct = preg_match('/^[6-9]\d{9}$/', $_POST['billing_phone']);
if ( $_POST['billing_phone'] && !$is_correct) {
wc_add_notice( __( 'The Mobile No. should be <strong>10 digits with starting 6,7,8,9</strong>.Try Again.' ), 'error' );
}
}
发布于 2018-12-07 11:10:46
在我看来,没有必要覆盖默认Woocommerce字段,也不需要使用Javascript验证。您可以添加一个自定义验证,该验证将添加到Woocommerce的默认计费电话字段验证中,并与提交结帐后触发的操作挂钩。
这是我刚为客户端实现的代码。
// Custom 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]{6,20}$/', $_POST['billing_phone']);
if ( $_POST['billing_phone'] && !$is_correct) {
wc_add_notice( __( 'The Phone field should be <strong>between 6 and 20 digits</strong>.' ), 'error' );
}
}
当然,与我的preg_match不同,您可以检查其他任何内容,并根据需要调整条件代码。
当然,您还可以在正确设置正确的$_POST变量或您自己的自定义签出字段之后,为其他默认字段添加自定义验证,但这是另一个主题:)
希望这能有所帮助。
https://stackoverflow.com/questions/53667082
复制相似问题