How to add US phone number format in WooCommerce checkout

Phone number masking is crucial for safeguarding user privacy and security in various online interactions. By concealing personal contact details, it prevents potential misuse, spam, and harassment. This practice not only builds user trust but also ensures compliance with data protection laws and industry standards. Implementing phone number masking not only enhances the professionalism of businesses but also contributes to a seamless user experience by minimizing friction in communication while maintaining confidentiality. Overall, phone number masking plays a vital role in promoting user safety, trust, and compliance in the digital landscape.

To ensure that the phone number field in your WooCommerce checkout is formatted as a US phone number, you can use a combination of WooCommerce hooks and some custom JavaScript. This involves two main steps:

  1. Add a custom JavaScript script to format the phone number.
  2. Add the script to your WooCommerce checkout page.

Step 1: Add the JavaScript code

First, you need to add a JavaScript code that will format the phone number input. This script will utilize a library or a custom function to format the input as a US phone number (###) ###-####.

Add the following JavaScript code using any code snippet plugin.

  // Function to format the phone number
  function formatPhoneNumber(value) {
    const phoneNumber = value.replace(/[^\d]/g, '');
    const phoneNumberLength = phoneNumber.length;
    if (phoneNumberLength < 4) return phoneNumber;
    if (phoneNumberLength < 7) {
      return `(${phoneNumber.slice(0, 3)}) ${phoneNumber.slice(3)}`;
    return `(${phoneNumber.slice(0, 3)}) ${phoneNumber.slice(3, 6)}-${phoneNumber.slice(6, 10)}`;

  $('#billing_phone').on('input', function() {
    const formattedInputValue = formatPhoneNumber(this.value);

This script listens for input on the #billing_phone field (the standard WooCommerce billing phone field ID) and formats it as a US phone number.

Step 2: Add the PHP Script in WordPress

Now, you need to add this PHP script so that it is loaded on the WooCommerce checkout page. Add the following PHP code to your child theme’s functions.php or add using a code snippet plugin.

function custom_enqueue_checkout_scripts() {
  if (is_checkout()) {
    wp_enqueue_script('custom-phone-format', get_template_directory_uri() . '/js/custom-phone-format.js', array('jquery'), '', true);
add_action('wp_enqueue_scripts', 'custom_enqueue_checkout_scripts');

This code checks if the current page is the checkout page and, if so, adds the custom script that we have shown in Step 1.

Additional Tweaks: Add a Placeholder

To add a placeholder with the format (xxx) xxx-xxxx to the phone number field in your WooCommerce checkout page, you can achieve this by using a WooCommerce filter to modify the billing fields. This can be done in conjunction with the JavaScript approach previously described for formatting the phone number input.

Here’s how you can add the placeholder to the phone number field:

Step 1: Add Placeholder via WooCommerce Hook

You’ll need to add some code to your child theme’s functions.php or add using a code snippet plugin. This code uses a WooCommerce filter to modify the billing fields, specifically to add a placeholder to the phone number field.

function custom_woocommerce_billing_fields($fields) {
    $fields['billing_phone']['placeholder'] = '(xxx) xxx-xxxx';
    return $fields;
add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');

Step 2: Ensure the JavaScript File is added

Make sure that the JavaScript file you created to format the phone number input (Step 1) is added correctly as described in the previous instructions. This script will work alongside the placeholder to ensure a consistent user experience.

Complete Solution

By combining the modified functions.php code with the JavaScript solution provided earlier, you achieve two things:

  1. Placeholder Display: The placeholder (xxx) xxx-xxxx is displayed in the phone number input field, guiding users on how to enter their phone number.
  2. Input Formatting: As users type their phone number, the JavaScript formats the input in real-time to match the US phone number format (###) ###-####.

The Final Result

This is how it will look after you use adding these codes

Final Recommendations

  • Test the Changes: Ensure that you test these changes on a staging or development version of your site first. This helps to avoid any potential issues on your live website.
  • Consider User Experience: While formatting and placeholders can improve user experience, ensure they work seamlessly across different browsers and devices.

This approach should provide a more intuitive and guided experience for users entering their phone numbers during the WooCommerce checkout process.

Final Notes

  • This approach uses basic JavaScript for formatting; consider using a library like libphonenumber-js for more robust formatting options, including automatic country detection and formatting.
  • Always test changes on a staging environment before applying them to your live site to ensure there are no unforeseen issues with your specific setup.

This method will help ensure that users input their phone numbers in the correct US format during the WooCommerce checkout process.

Leave a Reply

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