Convert unformatted number string into US telephone number

I often capture telephone numbers as just a string of numbers, like 4551234567. Is there an action to format that string into a proper telephone number like (455) 123-4567 ?

I don’t need validation or other functions, just apply the parentheses, space, and hyphen to selected text.

Would it be best to use regex, or a script?

Thanks so much to the community!

I would suggest a script that utilises regular expressions. That is not a flippant response, simply that you are going to need both to address the challenge.

Presumably this is some sort of special formatting for your country as it isn’t a global standard for formatting of telephone numbers, but regardless the issue is not necessarily as straight forward as it might appear. This is because not all ten digit strings of digits are necessarily telephone numbers.

We can limit the cases quite quickly using word boundaries in a regular expression, but that approach will not account for all cases as I’ll show below.

Let’s start with a test case:

I often capture telephone numbers as just a string of numbers, like 4551234567. Is there an action to format that string into a proper telephone number like (455) 123-4567 ?

What about on its own line?
1234567890

What if it is a longer number?
123456789012345678901234567890

What happens if it is a similar length number with a decimal point?
1234567890.0

What happens if it is not a number?
1234567890-ABCDEF

 

If we then run the following regex-based lines of script to do a substitution against ten digits spanned by word boundaries, we get the result below.

draft.content = draft.content.replaceAll(/\b(\d{3})(\d{3})(\d{4})\b/g, "($1) $2-$3");
draft.update();

 

I often capture telephone numbers as just a string of numbers, like (455) 123-4567. Is there an action to format that string into a proper telephone number like (455) 123-4567 ?

What about on its own line?
(123) 456-7890

What if it is a longer number?
123456789012345678901234567890

What happens if it is a similar length number with a decimal point?
(123) 456-7890.0

What happens if it is not a number?
(123) 456-7890-ABCDEF

 

It works okay for matching in amongst text, on new lines, and doesn’t match longer strings of numbers. However, it will match other sequences where you wouldn’t want it to. That is where I would start looking to apply some additional scripting.

If I was confident I wouldn’t catch any other cases, I’d use the two lines of script. If there was a chance of something more, I’d come up with a more sophisticated solution to deal with the edge cases.

The underlying issue is that the thing a human would use to recognise a string of digits as an unformatted telephone number is context. You effectively have to code that context yourself for Drafts to be able to apply it, but it isn’t an easy problem. You may have noticed if you have a call handler turned enabled (most mobile browsers incorporate this by default) that sometimes it allows you to tap and call a number that is not a telephone number.

Hopefully, that’s enough to get you started.

1 Like

I appreciate this input sylumer. What I’m trying to do is even simpler than that. I don’t need the script to search the whole document, or to pick out what might be a phone number.

My notes in this use case are usually just a name and a phone number:

John Smith
1234567890

All I want to do is manually select the number (which will always be a 10 digit string) and replace it with a US-formatted phone number:

John Smith
(123) 456-7890

I’ve already stripped down your regular expression to what I think I need:

\d{3})(\d{3})(\d{4}

What I don’t know how to do is replace the selected text with the formatted string. I’m pretty sure there’s a “getSelectedText” command in there somewhere, and a “setSelectedText” as well.

I’m not even sure this needs to be a script, since it’s really just replacing selected text, but I don’t actually know that for sure.

I appreciate your patience, and your willingness to help.

P.S. I’m a big fan of your TAD action groups!

In that case what I created above should work perfectly for you, and save you having to even do the manual selection. A more efficient solution :nerd_face:

As noted above, you do not need to do that. The existing code would be more efficient as it eliminates any requirement to manually select the number.

But, for reference, the functions you would want for work g with text selections are part of the Editor class.

You might be able to do this using the built in find and replace, but you would have to set it up each time. A script is perfect for meeting this requirement.

No problem. There are many helpful people on the forum, and I’m glad you are getting some use from the actions I created.