Change phone number to different format

I do not know how to achieve what I want. I'm sure one of you can help. :slightly_smiling_face:

I want to be able to have a form that I put a phone number in any of these formats, (555) 555-555, 555555555, 555-555-5555, and then have the formats produced from the input into all 3 formats.
If that isn't achievable with one form, then it would be ok to have 3 forms to input the different formats.

So, {textform}=a phone number in any of those formats

(555) 555-5555
555-555-5555
5555555555

1 Like

Hi @Laren_Eggleston Hope you are doing good today.

I think that this can be solved in multiple ways, taking the example you mentioned, I used the string length and the substring and contact functions to process the input.

I think that this can also be done with Regular expressions, but for now I think that this can help.

Please insert a phone number = {formtext: name=Phone_number}

{if: len(Phone_number) == 10}Inserted Format = {=phone_number}
Second Format= {=concat(substring({=phone_number}, 1, 3),"-", substring({=phone_number}, 4, 3),"-" ,substring({=phone_number}, 7, 4))}
Third Format= {=concat("(",substring({=phone_number}, 1, 3),") ", substring({=phone_number}, 4, 3),"-", substring({=phone_number}, 7, 4))}{elseif: len(Phone_number) == 12}Inserted Format = {=phone_number}
Second Format= {=concat(substring({=phone_number}, 1, 3), substring({=phone_number}, 5, 3), substring({=phone_number}, 9, 4))}
Third Format= {=concat("(",substring({=phone_number}, 1, 3),") ", substring({=phone_number}, 5, 3),"-",substring({=phone_number}, 9, 4))}{elseif: len(Phone_number) == 14}Inserted Format = {=phone_number}
Second Format= {=concat(substring({=phone_number}, 2, 3), substring({=phone_number}, 7, 3), substring({=phone_number}, 11, 4))}
Third Format= {=concat(substring({=phone_number}, 2, 3),"-", substring({=phone_number}, 7, 3),"-", substring({=phone_number}, 11, 4))}{else}Please insert a valid phone number format {endif}

Please let me know if you have any question. Hope this helps.

Best regards,

Diego,

1 Like

Thank you @Diego_Suancha ! This really helps!

2 Likes

Great snippet @Diego_Suancha!

@Laren_Eggleston - here's another way to do it:

{note}
This is the text that should contain the number:
{formparagraph: name=text; cols=100; rows=5; default=This is a phone number: 1234567890

However, this is not a phone number: 12345}

This is the number: {number=extractregex(text, "\d{10}")}{=number}

These are the digits extracted from the number: {digits=extractregexall(number, "\d")}{=digits}

{endnote: trim=right}

({=join(slice(digits, 1, 3), "")}) {=join(slice(digits, 4, 6), "")}-{=join(slice(digits, 7, 10), "")}
{=join(slice(digits, 1, 3), "")}-{=join(slice(digits, 4, 6), "")}-{=join(slice(digits, 7, 10), "")}
{=join(digits, "")}

Both methods are fine. Pick whichever one you prefer, but try to understand what's happening in both, so you can replicate the various strategies when building other snippets.

Any questions, I'm happy to help, and I'm sure the same goes for @Diego_Suancha :slight_smile:

2 Likes

Thank you Cedric! Diego's solution helped me, but your solution was what I had been trying to figure out how to do it. I appreciate the helpful information! :smiley:

1 Like

@Laren_Eggleston - glad you found the solutions helpful. Make sure you tear them apart and try different methods of breaking them. That's the best way to learn how they work :slight_smile:

I am working on reverse engineering them Cedric. Thank you!

1 Like

Have fun :grin:

@Cedric_Debono_Blaze I have taken what you have given and learned a lot while working it into what I want it to be. However, I have a couple problems.
I want to only show the results from whichever is detected. I tried if statements, but I couldn't get them to work.
The other problem is that I would like the last numbers to have the +1 in front of all of the numbers, +15555555555. I have the +1 outside, but I figured that there must be a way to put it inside. Maybe using an & or something.

For the +1, I'd say just put it outside—much simpler that way.

For detecting all three variants, you could use testregex together with an if command. So:

{formtext: name=text}

{if: testregex(text, "\d{10}")}{number=extractregex(text, "\d{10}")}{elseif: testregex(text, "\d{5}-\d{5}")}{number=extractregex(text, "\d{5}-\d{5}")}{elseif: testregex(text, "\d{3}-\d{3}-\d{4}")}{number=extractregex(text, "\d{3}-\d{3}-\d{4}")}{endif}
{=number}

Then continue with the rest of the snippet that I gave you :slight_smile:

Hi Cedric. I've been trying to figure out how to extract any numbers regardless of how the numbers are entered.
Ex: 555-555-5555 should be extracted to 5555555555
(555) 555-5555 should also be extracted to 5555555555

So I have been trying to figure out how to extract anything that's a number and ignore anything that's not. In the Formula reference it says [^xyz] will ignore those letters. So I thought that I could [^()-] to get only the numbers. However, I've tried several times and have not been successful yet.

Hi Laren,

This should do it :slight_smile:

{formparagraph: name=data; cols=80; rows=3; default=Ex: 555-555-5555 should be extracted to 5555555555
(555) 555-5555 should also be extracted to 5555555555}
{=replace(replace(replace(extractregexall(data, "\(?\d+\)? ?-?\d+-?\d+"), "-", ""), "(", ""), ") ", "")}

Not sure maybe @scott has a more elegant solution.

1 Like

Thank you @Cedric_Debono_Blaze . That does work. This is something that I'm doing more for learning than out of need. So any other thoughts on that would be welcome. :slightly_smiling_face:

@Laren_Eggleston - I understand completely. I'm always pushing myself to discover better ways to use Text Blaze, which is why I love challenges like the ones you and other users post. Keep them coming!

And if you ever wanna jump on a call to bounce a few ideas around, don't hesitate to let me know. Besides being my job, it's also something I enjoy immensely. Hit me up anytime.

1 Like