WooCommerce:强制执行最小长度电话号码字段 [英] WooCommerce: Enforce minimum length phone number field

查看:26
本文介绍了WooCommerce:强制执行最小长度电话号码字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在基于 WordPress 的网站中安装了 WooCommerce.现在我的问题是当客户结帐或创建 ID 时,有一个字段可供用户插入他的电话号码.该字段接受 9 个数字,因此我想在该字段上应用最小长度函数,以便用户收到错误消息提示.

I have installed WooCommerce in my WordPress based website. Now my problem is when a customer checks out or creates an ID, then there is a field where the user can insert his phone number. That field accepts 9 numbers, so I want to apply a minimum length function on that field so the user will be prompted with an error message.

我尝试在 function.php 中添加这些行:

I have tried by adding these lines in function.php:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields )
{        
     $fields['billing']['billing_phone']['minlength'] = 10;      
     return $fields;    
}

但这不起作用,奇怪的是当我使用

but this is not working and the strange thing is that when I use

['maxlength'] = 10; 

它确实有效.

推荐答案

默认情况下,WooCommerce 结帐字段支持字段的以下属性

By default WooCommerce checkout fields support the following attributes for the fields

$defaults = array(
    'type'              => 'text',
    'label'             => '',
    'description'       => '',
    'placeholder'       => '',
    'maxlength'         => false,
    'required'          => false,
    'id'                => $key,
    'class'             => array(),
    'label_class'       => array(),
    'input_class'       => array(),
    'return'            => false,
    'options'           => array(),
    'custom_attributes' => array(),
    'validate'          => array(),
    'default'           => '',
);

您可以通过将数组传递给 custom_attributes

You can add custom attributes by passing an array to custom_attributes

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields )
{        
     $fields['billing']['billing_phone']['custom_attributes'] = array( "minlength" => "12" );      
     return $fields;    
}

这将产生以下 HTML

Which would produce the following HTML

<input type="text" minlength="12" value="" placeholder="" id="billing_phone" name="billing_phone" class="input-text ">

如果 minlength 不起作用(我怀疑它可能不会起作用),请尝试使用 pattern 属性

If minlength doesn't work ( and I have a suspicion that it probably won't ) , try using the pattern attribute

$fields['billing']['billing_phone']['custom_attributes'] = array( "pattern" => ".{12,}" );//最少12个字符

这篇关于WooCommerce:强制执行最小长度电话号码字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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