• Resolved noumantuheed

    (@noumantuheed)


    I want to set the limit on Phone Number field it’s not more than 11 or less than 11

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,

    Add this piece of code to functions.php or use a plugin or use your own written plugin

    // Set maxlength
    function my_custom_override_checkout_fields( $fields ) {
        $fields['billing']['billing_phone']['maxlength'] = 11;
        return $fields;
    }
    add_filter( 'woocommerce_checkout_fields' , 'my_custom_override_checkout_fields' );
    
    // Limit Woocommerce phone field to 11 digits number
    function my_custom_checkout_field_process() {
        global $woocommerce;
    
        // Check if set, if its not set add an error. This one is only requite for companies
        if ( ! (preg_match('/^[0-9]{11}$/D', $_POST['billing_phone'] ))){
            wc_add_notice( "Incorrect Phone Number! Please enter valid 11 digits phone number"  ,'error' );
        }
    }
    add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

    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'           => '',
    );
    Plugin Support AW a11n

    (@slash1andy)

    Automattic Happiness Engineer

    Hey there!

    Marking as resolved, as the solution above would be the way to do that, if your theme is not restricting that.

    Thread Starter noumantuheed

    (@noumantuheed)

    Thanks It’s solved now

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How To Set Limit On Phone Number Field’ is closed to new replies.