10 Digit Limit in Phone Checkout Field in WooCommerce For Validation

users making fake orders in WooCommerce powered eCommerce stores is quite common. If you are using WooCommerce to get phone number leads, sorting through those fake or invalid leads can be time consuming.
To overcome this problem, you can force validate the woocommerce checkout process to limit the phone number field to have 10 digit valid phone number. With this if the entered number is less than 10 digits then checkout process cannot be completed and it will show an error message to the users to enter the correct number.
Code snippet down below can be used to limit checkout phone field in Woocommerce checkout to 10 numbers.

// Limit Woocommerce phone field to 10 digits number
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
  
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]{10}$/D', $_POST['billing_phone'] ))){
        wc_add_notice( "Incorrect Phone Number! Please enter valid 10 digits phone number"  ,'error' );
    }
}
10 Digit Limit in Phone Checkout Field in WooCommerce For Validation 10 Digit Limit in Phone Checkout Field in WooCommerce For Validation Reviewed by webmission on 13:28 Rating: 5

No comments:

Powered by Blogger.