# Formatt phone number

**URL:** https://community.glideapps.com/t/formatt-phone-number/63282
**Category:** Ask for Help
**Created:** [June 29, 2023, 12:15am UTC](https://community.glideapps.com/t/formatt-phone-number/63282 "2023-06-29T00:15:12Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![John\_Romano](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/john_romano/32/74316_2.png) [@John\_Romano](https://community.glideapps.com/u/John_Romano)
#### Post date: [June 29, 2023, 12:15am UTC](https://community.glideapps.com/t/formatt-phone-number/63282/1 "2023-06-29T00:15:12Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Eric\_Penn](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/eric_penn/32/73888_2.png) [@Eric\_Penn](https://community.glideapps.com/u/Eric_Penn)
#### Post date: [June 29, 2023, 12:23am UTC](https://community.glideapps.com/t/formatt-phone-number/63282/2 "2023-06-29T00:23:53Z")

</div>

Here’s a gem 💎 from Darren

> [@I need help with regular expressions and formatting](https://community.glideapps.com/t/i-need-help-with-regular-expressions-and-formatting/62863/3):
>
> I started having a look at this yesterday, and ended up going down a bit of a rabbit hole, so I put it aside. Had another look just now and it occurred to me that all you really need to do is strip out all the non-digits and then return the last 9 characters of whatever is remaining. So with that in mind, here is a bit of rough and ready JavaScript that does that. I’m sure it could be improved. const parts = p1.split(''); let digits = []; while (parts.length \> 0) { let bit = parts.shift(); …

---

<div class="post-metadata">

### Author: ![John\_Romano](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/john_romano/32/74316_2.png) [@John\_Romano](https://community.glideapps.com/u/John_Romano)
#### Post date: [June 29, 2023, 1:44am UTC](https://community.glideapps.com/t/formatt-phone-number/63282/3 "2023-06-29T01:44:33Z")

</div>

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(‘’);

---

<div class="post-metadata">

### Author: ![gvalero](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gvalero/32/1037_2.png) [@gvalero](https://community.glideapps.com/u/gvalero)
#### Post date: [June 29, 2023, 2:17am UTC](https://community.glideapps.com/t/formatt-phone-number/63282/4 "2023-06-29T02:17:18Z")

</div>

Hola John,

Try with this code…

```auto
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

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/7/2/7212c987643826176d934ac2e04d59c11701a5e0.png)

Saludos!

---

<div class="post-metadata">

### Author: ![Eric\_Penn](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/eric_penn/32/73888_2.png) [@Eric\_Penn](https://community.glideapps.com/u/Eric_Penn)
#### Post date: [June 29, 2023, 3:12am UTC](https://community.glideapps.com/t/formatt-phone-number/63282/5 "2023-06-29T03:12:09Z")

</div>

> [@John\_Romano](#):
>
> I am a versatile NO CODE builder and I have zero experience with a column type like this

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

---

<div class="post-metadata">

### Author: ![John\_Romano](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/john_romano/32/74316_2.png) [@John\_Romano](https://community.glideapps.com/u/John_Romano)
#### Post date: [June 29, 2023, 1:16pm UTC](https://community.glideapps.com/t/formatt-phone-number/63282/6 "2023-06-29T13:16:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![gvalero](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gvalero/32/1037_2.png) [@gvalero](https://community.glideapps.com/u/gvalero)
#### Post date: [June 29, 2023, 5:41pm UTC](https://community.glideapps.com/t/formatt-phone-number/63282/7 "2023-06-29T17:41:49Z")

</div>

Sure!

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

```auto
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!

---

<div class="post-metadata">

### Author: ![John\_Romano](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/john_romano/32/74316_2.png) [@John\_Romano](https://community.glideapps.com/u/John_Romano)
#### Post date: [June 30, 2023, 12:24pm UTC](https://community.glideapps.com/t/formatt-phone-number/63282/8 "2023-06-30T12:24:14Z")

</div>

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 :::—)))

---

<div class="post-metadata">

### Author: ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)
#### Post date: [June 30, 2023, 12:41pm UTC](https://community.glideapps.com/t/formatt-phone-number/63282/9 "2023-06-30T12:41:41Z")

</div>

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

return string2 + string3 + string4

---

<div class="post-metadata">

### Author: ![John\_Romano](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/john_romano/32/74316_2.png) [@John\_Romano](https://community.glideapps.com/u/John_Romano)
#### Post date: [June 30, 2023, 1:29pm UTC](https://community.glideapps.com/t/formatt-phone-number/63282/10 "2023-06-30T13:29:59Z")

</div>

THANK YOU VERY MUCH JEFF! I appreciate your help!

---

<div class="post-metadata">

### Author: ![gvalero](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gvalero/32/1037_2.png) [@gvalero](https://community.glideapps.com/u/gvalero)
#### Post date: [June 30, 2023, 3:05pm UTC](https://community.glideapps.com/t/formatt-phone-number/63282/11 "2023-06-30T15:05:35Z")

</div>

> [@John\_Romano](#):
>
> and if we ever meet the first whopper from BK is on me…

ok… but don’t forget the Pepsi or Coke 😉

Enjoy the code!

Feliz día…

---

<div class="post-metadata">

### Author: ![Info\_Appolon](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/info_appolon/32/74053_2.png) [@Info\_Appolon](https://community.glideapps.com/u/Info_Appolon)
#### Post date: [April 24, 2024, 10:28pm UTC](https://community.glideapps.com/t/formatt-phone-number/63282/12 "2024-04-24T22:28:33Z")

</div>

![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/d/0/d099c3081dfedd21f316374227932e3deb19bfec.png)  
Am I doing something wrong or were there some changes in glide that affect the way to format it?

---

<div class="post-metadata">

### Author: ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)
#### Post date: [April 24, 2024, 11:44pm UTC](https://community.glideapps.com/t/formatt-phone-number/63282/13 "2024-04-24T23:44:59Z")

</div>

Please try this.

```auto
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](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/0/8/08396af0cbdf0c8badd324e6f3ccb7d5aaa8c917.png)
