Formatt phone number

I want to be able to take a phone number input and strip it down to just raw numbers and then reformatt it. That is what I am thinking the flow should be after we collect.

A one two punch with formula columns. For instance…

If a sales rep opens a form and enters any number like this
{{— 15045551212, or, 1(504)555-1212, or, (504) 555-1212, or, 504-555-1212, or, 1-504-555-1212, ect…—}}

take any of those and…

1st:Strip a leading one away from it if it is present & remove any spaces or special characters out and,

2nd: reformatt to this (504) 555-1212 every time.

Here’s a gem :gem: from Darren

Thank you! How do I implement this? I am a versatile NO CODE builder and I have zero experience with a column type like this…I just need a little head start on what column and how to input the code below and start to test it out…but it seems pretty incredible…

const parts = p1.split(‘’);
let digits = ;
while (parts.length > 0) {
let bit = parts.shift();
if (bit.match(/\d/)) {
digits.push(bit);
}
}
return digits.slice(-9).join(‘’);

Hola John,

Try with this code…

var  phonePattern = /\d+/g;
let phoneText= p1.match(phonePattern).join('');

let len=phoneText.length;
let string4=phoneText.slice(len-4,len);
let string3=phoneText.slice(len-7,len-4);
let string2=phoneText.slice(len-10,len-7);
let string1=phoneText.slice(0,len-10);

return string1+ " (" + string2+ ") " +string3+ "-" + string4

If you want to remove the International Calling Codes, remove everything associated to string1 variable in my code

Saludos!

1 Like

A little bit of code won’t hurt :wink:. Use the Java script column

that worked besides removing a 1 if found in the beginning…can you give me that?

Sure!

use this if you don’t want to use International Calling Codes:

var  phonePattern = /\d+/g;
let phoneText= p1.match(phonePattern).join('');

let len=phoneText.length;
let string4=phoneText.slice(len-4,len);
let string3=phoneText.slice(len-7,len-4);
let string2=phoneText.slice(len-10,len-7);

return  "(" + string2+ ") " +string3+ "-" + string4

Chao!

2 Likes

Thanks man that worked perfect…could I ask you for one more favor, and if we ever meet the first whopper from BK is on me… Can you give me a formula that removes a leading 1 if it is present and all special characters and spaces also. I need this for a webhook that i send these to that the api on the other end of the webhook will not accept nothing but that. 1 (504) 555-1212 to 5045551212

Much obliged sir :::—)))

1 Like

You can just change the Return and remove the special characters.

return string2 + string3 + string4

2 Likes

THANK YOU VERY MUCH JEFF! I appreciate your help!

2 Likes

ok… but don’t forget the Pepsi or Coke :wink:

Enjoy the code!

Feliz día…


Am I doing something wrong or were there some changes in glide that affect the way to format it?

Please try this.

p1 = p1.toString(); // convert p1 to a string
p1 = p1.replace(/\D+/g, ''); // remove all non-digit characters
if (p1.length !== 10) {
    return('Invalid phone number');
} else {
    return('(' + p1.substring(0, 3) + ') ' + p1.substring(3, 6) + '-' + p1.substring(6));
}

image

3 Likes