php - WooCommerce: Enforce minimum length phone number field -


i have installed woocommerce in wordpress based website. problem when customer checks out or creates id, there field user can insert phone number. field accepts 9 numbers, want apply minimum length function on field user prompted error message.

i have tried 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 not working , strange thing when use

['maxlength'] = 10;  

it works.

by default woocommerce checkout fields support following attributes 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'           => '', ); 

you can add custom attributes passing array 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;     } 

which produce following html

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

if minlength doesn't work ( , have suspicion won't ) , try using pattern attribute

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


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -