WooCommerce: Phone Input Mask @ Checkout

The WooCommerce checkout page has a default phone input field that gets validated upon checkout (for HTML geeks, it’s actually an input type = “tel”). Usually, if such phone number contains letters, it will fail and checkout will stop.

But that’s not the problem. Let’s suppose you only have US customers, or that you want to force customer to enter a certain format (e.g. “+” or “01” as prefix)… well, there is no way to achieve that out of the box within the WooCommerce settings.

However, anything is possible in regard to customization, so all we need are 3 small changes: force the billing phone input to stay within a “maxlength“, set the phone input placeholder so that users know what the format should be before typing in, and finally add some JavaScript to provide an input “mask”, so that the final output is exactly what we want (123-456-7890 in this case scenario, but it could be anything).

Enjoy!

Once the snippet below is active, the phone input will display a placeholder “123-456-7890” and also, once you start typing in it, it will stick to that format and won’t allow you to change it. It also won’t allow you to enter more than 12 characters because we’ve set a maxlength.

PHP Snippet: Phone Placeholder and Input Mask @ WooCommerce Checkout

/**
 * @snippet       Phone Mask @ WooCommerce Checkout
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_checkout_fields', 'bbloomer_checkout_phone_us_format' );
  
function bbloomer_checkout_phone_us_format( $fields ) {
    $fields['billing']['billing_phone']['placeholder'] = '123-456-7890';
	 $fields['billing']['billing_phone']['maxlength'] = 12; // 123-456-7890 is 12 chars long
    return $fields;
}

add_action( 'woocommerce_after_checkout_form', 'bbloomer_phone_mask_us' );

function bbloomer_phone_mask_us() {
	wc_enqueue_js( "
		$('#billing_phone')
		.keydown(function(e) {
			var key = e.which || e.charCode || e.keyCode || 0;
			var phone = $(this);			
			if (key !== 8 && key !== 9) {
			  if (phone.val().length === 3) {
				phone.val(phone.val() + '-'); // add dash after char #3
			  }
			  if (phone.val().length === 7) {
				phone.val(phone.val() + '-'); // add dash after char #7
			  }
			}
			return (key == 8 ||
			  key == 9 ||
			  key == 46 ||
			  (key >= 48 && key <= 57) ||
			  (key >= 96 && key <= 105));
		  });
		  
	" );
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Add “Confirm Email Address” Field @ Checkout
    A correct email address is worth a thousand dollars, some ecommerce expert would say ๐Ÿ™‚ So, you don’t want your WooCommerce checkout visitors to mess up with that, do you? What about adding an “Email Verification” field? In this way, we can make sure they double check their entry – and also show an error […]
  • WooCommerce: Hide Checkout Fields if Virtual Product @ Cart
    If you sell downloadable/virtual products and need to simplify your WooCommerce checkout when such product type is in the Cart, you’ve come to the right place! Here’s a simple snippet, as well as a handy mini-plugin, that checks if there are only “virtual” products in the Cart and hides all the billing fields and order […]
  • WooCommerce: How to Add a Custom Checkout Field
    Let’s imagine you want to add a custom checkout field (and not an additional billing or shipping field) on the WooCommerce Checkout page. For example, it might be a customer licence number – this has got nothing to do with billing and nothing to do with shipping. Ideally, this custom field could show above the […]
  • WooCommerce: Move / Reorder Checkout Fields (Email, Country, etc.)
    We’ve already seen how to disable fields on the checkout page by using a simple snippet. Given my ultimate goal of trying to do as much as possible without installing heavy-weight plugins, today we’ll take a look at how to move fields around inside the billing & shipping sections.
  • WooCommerce: Add House Number Field @ Checkout
    A North European client told me they’re really strict about billing and shipping addresses over there. Couriers usually require a separate “House Number” in order to dispatch packages within those countries. This must be therefore placed on the checkout, BESIDE the “Address_1” field and made required. Also, it’s a good idea to make this show […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

13 thoughts on “WooCommerce: Phone Input Mask @ Checkout

  1. Having trouble with this. It’s giving me syntax error , unexpected ‘function’ (T-FUNCTION).

    1. Hi Heather I get no such error, so maybe it’s a copy/paste issue on your end?

  2. This code does not work when the browser autofill’s phone number?

    1. I’m pretty sure there is a way to disable “autocomplete” on the phone field – I’ll see if I can post a tutorial when I get some time

      1. Also, is this for logged in or logged out customers?

        1. Both logged in and non-logged in customers. Have you figured out a way to disable auto complete?

  3. Thanks Man

  4. If I have clients from different country how to create mask depends on different country ?
    Ex. Israel +972 xx xxx xxxx
    Russia +7 xxx xxx xxxx

    1. Hello Rina thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  5. I’m glad I’m subscribed to your newsletter.. Every week I learn something new, Thanks Rodolfo!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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