WooCommerce Only Allow Number Input For Billing Phone

WooCommerce checkout phone number field only allowing numbers only

Pre-Requisites

There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About this Solution

This snippet adds jQuery (run form the footer for performance reasons) which only allows number input in the billing phone field on the checkout. Specifically, it will find and delete anything which is not a number. This means the user will be unable to add any special characters, spaces, or letters in the billing phone field. This acts as an input mask rather than a validation method – the user simply will not be able to enter anything but numbers in the field.

				
					/**
 * Snippet Name:	WooCommerce Only Allow Number Input For Billing Phone
 * Snippet Author:	ecommercehints.com
 */

add_action('wp_footer', 'ecommercehints_billing_phone_validation');
function ecommercehints_billing_phone_validation() {
        if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        $('#billing_phone').on( 'input focusout', function() {
            var p = $(this).val();
            $(this).val($(this).val().replace(/[^0-9]/g, ''));
        });
    });
    </script>
    <?php
    endif;
}
				
			

Snippet Benefits

  • Force consistent phone numbers through the WooCommerce checkout.
WooCommerce Conversion Rate Optimisation (CRO) eBook
100 WooCommerce Conversion Rate Optimisation Tips

Leave a Reply

If you are going to write code in the comments, please wrap it between code tags.

Your email address will not be published. Required fields are marked *

Other Recent Guides

Subscribe To Emails

Get exclusive WooCommerce tips that I only share with email subscribers

Join hundreds of other subscribers