Hey everyone, I’m pretty new to JavaScript and could really use some guidance!
I’m working on a Zapier workflow where I need to process phone numbers that come through a webhook. The incoming phone numbers are in this format: 5556667777 (just 10 digits with no formatting).
What I need to do is:
Validate that the phone number has exactly 10 digits
Add a 1 at the beginning to make it 15556667777
Use this formatted number for my Twilio SMS integration
I’ve been trying to write some JavaScript code in Zapier’s Code step but I’m struggling with the validation and formatting logic. Has anyone done something similar before? What’s the best approach to handle this kind of phone number transformation?
Any code examples or tips would be super helpful. Thanks!
try this quick fix - check if input.length === 10, then just do ‘1’ + phoneNumber. had the same problem with zapier last month and this worked perfectly for twilio. just make sure you handle the output as a string, not a number, or zapier will screw it up
I ran into this exact issue with my Zapier workflows. Wrap everything in a try-catch block - it’ll save you tons of debugging time later. Phone validation breaks in weird ways when you get funky input data. I use parseInt() first to make sure I’m working with real numbers, check the length, then convert back to string for formatting. Heads up - some webhooks send phone numbers with leading zeros that get stripped. You might need to pad with zeros if the length is under 10 but over 7. Also check your Zapier output format settings. Twilio’s picky about data types.
I use regex to handle phone numbers in JavaScript - it’s pretty straightforward. Start with /^\d{10}$/ to check if you’ve got exactly 10 digits. Once that’s verified, just add ‘1’ to the front. I always clean the input first with replace(/\D/g, ‘’) to strip out anything that’s not a number. Keep the final result as a string for Twilio - don’t let it convert to a number type or you’ll run into issues.