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.