Code By Zapier: How to clean and format phone numbers

Ezra Sandzer-Bell
6 min readApr 11, 2020

So let’s say that your company has a web form set up to collect name, email, and phone numbers from site visitors. Maybe you have a demo that you want to offer and are collecting that data so your sales team can reach out. Zapier would typically act as the conduit for that webform data, helping it to flow into something like a CRM or SMS service. This use case is extremely common and Zapier does an excellent job solving for it, with one exception; phone number formatting.

To illustrate my point, think about how you personally write out your phone number. Do you use spaces, hyphens, or periods to separate the groupings? Do you include the country code or just the area code? And do you always write your phone number out the same way?

There is no single, universal format for phone numbers, which means that web form apps without proper data handling will allow your site visitors to type numbers willy-nilly with whatever format the heart desires.

Zapier knows that their users are processing phone number data, so they provide some excellent code-free data transformation tools. The best example would be their Processor app, which allows you to transform a string of numbers into a formatted phone number:

Zapier’s formatter tool lets you select a desired format and ISO code

As you can see in the screenshot above, the input value (section 2) is being provided a string of 9 digits with no additional characters. Additionally, the country code (section 4) is set to France. Since french numbers are nine digits long, Zapier will require a nine digit string of numbers in order to reformat for you correctly. Whatever country you choose, be sure to provide the correct number length, so that Zapier can parse it correctly. Here is what the output looks like for our current example:

Output for Zapier’s formatted phone number with French ISO code

Now that we’ve applauded Zapier for what it does well in phone number formatting, it’s time to talk about the feature’s limitation. As we’ve seen, you can turn number strings into formatted phone numbers as long as it has been stripped of all characters and has the correct string length. But a phone number like 07 75 62 21 25 (french format) will not be accepted because of its uses of spaces. The same would be true if there were parentheses and hyphens.

To handle all of the variations that you customers might throw at you, it’s going to be necessary to add a Regex script. If you’re not familiar with regular expressions, they are essentially a bit of code that allows you to transform an old string into a new one. In our case, we want to turn a form submission like “07 75 62 21 25” to “775622125”, so that it can be reformatted correctly.

What not to use: The Text Formatter & Extract Pattern

To clean up this string, we needed a way to extract numerals from the spaces parentheses and hyphens. We also knew that we wanted to pull out the last n digits of the number, so that country codes would be omitted by default and all numbers could be formatted the same way. The goal is to arrive at a number string that we can pass to the Zapier phone number formatting tool.

Initially we tried using a feature called Extract Pattern from the Text Formatter, because it supports a regex pattern that we hoped could transform our phone number input data. But Zapier only finds the first match for your regular expression before it quits, so you don’t get the full number in return.

Using Code by Zapier instead to clean up phone numbers

We contacted the Zapier support team with the above issue and they forwarded us to Code by Zapier. From here we selected Run Python from the list of options. This allows us to write a Python Regex expression on the phone number input data and output the string of numbers we wanted.

At the top of this modal, we set the Input Data name to phone and pulled the webform phone number data in as the value. Below, in the Code area, we did a few things. I’ll provide the code here so you can copy and paste, if needed.

import re
pattern = “[^\d]+”
cleaned = re.sub(pattern,””, input_data[“phone”])[-9:]
output = {“cleaned_phone”:cleaned}

NOTE: This regex code snippet is designed for 9 digit French phone numbers. The USA has 10 digit numbers, so their code snippet would need to be updated from [-9:] to [-10:] and the same logic should be followed for any country.

Technical details about why this Regex code works

First we imported “re”, Python’s built in Regular Expressions library, so that we could write regex code. Then we wrote our regex pattern as “[^\d]+" to match for one or more numerals [0–9].

Next, we used the python library method re.sub(), which takes three arguments; the first is your pattern, the second is an empty string to clear out anything that doesn’t conform, and finally input_data[“phone”] as our dictionary to pull in the unformatted web form number. At the end of the code, we added [-9:] to retrieve only the last nine digits of the extracted numeral series, since that is what we need in our example with french phone numbers.

Finally, we set the cleaned phone number to our output variable so that it knows where to go. As you can see here in the output screen, the formatting was removed and only the last 9 digits were assigned to our custom cleaned_phone variable.

Regex code snippet successfully cleans and extracts your number

Phone Number Formatting: Assembling your Zap

Now that we’ve shown you the essential components, we’re going to zoom out to take a look at the overall composition of your final Zap, which should include these four steps.

  1. The first step will be your web form, which in our case is Appointlet, an online scheduling app. You can use any web form app that Zapier supports.
  2. Next you will create a Code by Zapier step and add the code snippet we provided.
  3. Your third step will be to set up the Processor app with the Numbers helper, so you can access their Transform feature’s Format Phone Number option.
  4. Send your formatted phone number to an app of your choice, which could be Google Sheets, Hubspot, Twilio, or any number of other apps.

With this data flow set up, any phone number you receive from a customer should be processed into a single, consistent format. In a perfect world, your web form would provide the validation layer. But many apps lack that option and so this workaround should set you on the right path. Let us know if you run into any problems in the comment section below and we’ll do what we can to help!

--

--