【问题标题】:WooCommerce: Enforce minimum length phone number fieldWooCommerce:强制执行最小长度电话号码字段
【发布时间】:2015-10-17 16:05:41
【问题描述】:

我在基于 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; 

它确实有效。

【问题讨论】:

    标签: php regex wordpress validation woocommerce


    【解决方案1】:

    默认情况下,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}" );class="comcopy">跨度>
    【解决方案2】:

    终于成功了。我将以下代码放入我的模板中:

    $fields['billing']['billing_phone']['custom_attributes'] = array( "pattern" => ".{10,10}" );
    

    【讨论】:

      猜你喜欢
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      • 2010-12-19
      相关资源
      最近更新 更多