C
C
coverme2019-02-08 14:09:30
WooCommerce
coverme, 2019-02-08 14:09:30

How to validate phone number in woocommerce in this case?

I added the following mask to the phone input field on the checkout page
-

jQuery(document).ready(function($){
 $("input[name='billing_phone']").mask("+7(000) 000-0000");
});

How to enable validation check in this field correctly?
I put it like this -
// Custom validation for Billing Phone checkout field 
add_action('woocommerce_checkout_process', 'custom_validate_billing_phone'); 
function custom_validate_billing_phone() { 
    $is_correct = preg_match('^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$', $_POST['billing_phone']); 
    if ($_POST['billing_phone'] && !$is_correct) { 
     wc_add_notice(__('The Phone field should be <strong>between 6 and 20 digits</strong>.'), 'error'); 
    } 
}

But it does not work correctly. It also sends an order with 3 digits. Is it possible to somehow change this code so that it sends only numbers and not less than the number of the phone number, that is, min 10, max 11 roughly speaking.
Well, or some other hook to use. Tell me please. I tried different options, in one of the options it always indicated that the field was filled in incorrectly, in the other it sends anything (3-4 numbers and ok)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RealFikus, 2019-02-08
@RealFikus

Try DADATA . He will not only check the phone, but will also provide a lot of help about filling in other fields. There is a plugin for Wordpress, it works fast, via API.
In some cases, it interferes))). But basically - standardizes fillable fields in forms, as expected.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question