我已经在我的WordPress网站上安装了WooCommerce。现在我的问题是,当客户签出或创建ID时,有一个字段,用户可以在其中插入他的电话号码。该字段接受9个数字,因此我想在该字段上应用最小长度函数,以便用户将收到错误消息。
我试过在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;
}
但是这不起作用奇怪的是当我使用
时['maxlength'] = 10;
它确实有效
WooCommerce结帐字段默认支持以下属性
字段$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
传递数组来添加自定义属性
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
<input type="text" minlength="12" value="" placeholder="" id="billing_phone" name="billing_phone" class="input-text ">
如果minlength
不工作(我怀疑它可能不会),尝试使用pattern
属性
$fields['billing']['billing_phone']['custom_attributes'] = array( "pattern" => ".{12,}" ); //min 12 characters
终于成功了。我在模板中放入了以下代码:
$fields['billing']['billing_phone']['custom_attributes'] = array( "pattern" => ".{10,10}" );