SOLVED

Reformat a Phone Number - Trouble with Code

Go to solution
Mkoran
Level 1

Reformat a Phone Number - Trouble with Code

We generate a unique service phone number for users and it is generated in the following format: 11234567890. I am looking to use a program token to reformat it in the following layout: 1-123-456-7890.  I found some code here from @SanfordWhiteman blog (https://blog.teknkl.com/velocity-reformat-phone-numbers-remove-punctuation/) but I can't seem to get the code to populate correct. I'm positive I am missing something. Any thoughts?  Thank you in advance!

 

Code being used currently:

#set( $originalPhone = $lead.servicePhoneNumber )
## remove non-digits
#set( $unpunctuatedPhone = $unprefixedPhone.replaceAll("\D","") )
## format as 1-123-456-7890
#set( $displayPhone = $unpunctuatedPhone.replaceAll("(\d{1})(\d{3})(\d{3})(\d{4})","$1\-$2\-$3\-$4") )
## raw form in href, friendly form in text
<a class="mktNoTrack" href="tel:+1${unpunctuatedPhone}">${displayPhone}</a>

 

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Reformat a Phone Number - Trouble with Code

Remember to use the Syntax Highlighter (“Insert/Edit Code Sample”) when inserting code so it’s readable:

SanfordWhiteman_0-1715705015800.png

 

I edited your post this time.

 

Looks like you’re using the wrong variable name. Should be $originalPhone, not $unprefixedPhone, on line 3:

 

#set( $originalPhone = $lead.servicePhoneNumber )
## remove non-digits
#set( $unpunctuatedPhone = $originalPhone.replaceAll("\D","") )
## format as 1-123-456-7890
#set( $displayPhone = $unpunctuatedPhone.replaceAll("(\d{1})(\d{3})(\d{3})(\d{4})","$1\-$2\-$3\-$4") )
## raw form in href, friendly form in text
<a class="mktNoTrack" href="tel:+1${unpunctuatedPhone}">${displayPhone}</a>

 

 

View solution in original post

Tags (1)
1 REPLY 1
SanfordWhiteman
Level 10 - Community Moderator

Re: Reformat a Phone Number - Trouble with Code

Remember to use the Syntax Highlighter (“Insert/Edit Code Sample”) when inserting code so it’s readable:

SanfordWhiteman_0-1715705015800.png

 

I edited your post this time.

 

Looks like you’re using the wrong variable name. Should be $originalPhone, not $unprefixedPhone, on line 3:

 

#set( $originalPhone = $lead.servicePhoneNumber )
## remove non-digits
#set( $unpunctuatedPhone = $originalPhone.replaceAll("\D","") )
## format as 1-123-456-7890
#set( $displayPhone = $unpunctuatedPhone.replaceAll("(\d{1})(\d{3})(\d{3})(\d{4})","$1\-$2\-$3\-$4") )
## raw form in href, friendly form in text
<a class="mktNoTrack" href="tel:+1${unpunctuatedPhone}">${displayPhone}</a>

 

 

Tags (1)