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 @Santa_Laren 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!

@Santa_Laren - 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

@Santa_Laren - 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:

@Santa_Laren - 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

Hi Cedric! Hope all is well!

I found a way that I want to use this, but I want the results to be just the numbers. Currently, the results are encased in brackets and quotation marks. ["5555555555"]

Can you, or anyone else who wants to chime in, assist me with this?

Cedric has a lot more context about what's happening in this snippet so he might have an additional suggestion. But looking at it, it looks like the phone number results are being saved as a list, which has formatting ["like", "this"]. Since the snippet can accept multiple phone numbers in a single entry, that stores each phone number as an individual entry.

Adding a join( to the start of it will tell Text Blaze to parse the list as a string, separated by whatever value you choose. In this example, they're separated by a comma and a space.

{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}
{=join(replace(replace(replace(extractregexall(data, "\(?\d+\)? ?-?\d+-?\d+"), "-", ""), "(", ""), ") ", ""),", ")}

1 Like

Thanks Andrew! That works great for what I'm doing, except that I came across another need in this snippet as well. I am using it to fill in the phone number field in a softphone that requires the format to be +15555555555. With the snippet that you so kindly left for me, I can change most numbers directly to 5555555555 and have the +1 right before the extracted data. The only issue is when the phone number I copy and past has the +1 already there. I could just copy everything after the +1 but would like to be able to copy all of the phone number and either remove the +1 or add the +1 if it's not there and not add it if it is. Any thoughts on that?

The extract and replace functions that Cedric had originally built for you strip the + out of anything entered into that text box -- I couldn't make it output a number for me that started with +1.

that being said, I was able to get it to output numbers that started with the leading 1 (no plus sign), and some that didn't. I updated the snippet slightly to accommodate for that situation -- this version adds the +1 to any numbers without the leading 1, and only the + to any number that already starts with the leading 1.

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

{repeat: for x in listofnumbers}{if: startswith(x,1)}+{=x}{else}+1{=x}{endif}
{endrepeat}

1 Like

That's perfect! Thank you Andrew!