Phone Number Formatting Macro

Description

If you’ve ever had multiple people entering phone numbers into a field, you’ve no doubt discovered the varied formats they can provide. Perhaps you’re already enforcing field validation on it, which is a pretty good solution for the most part. But also, maybe you need to reformat that number for a page or get old data to look the same.

Rather than building out a viewtool to handle it, which you then need to compile and deploy in a plugin, you can use something like this macro example. Just pass it a phone number string (such as “1-555-555-1234″ or “555.555.1234,” it can handle different punctuation – that’s the point) and it does the rest. It defaults to US numbers, but you could easily change that or pass a different $phoneCountry value. If you want to include the country code in the number, just set $addCountryCode to true before using the macro (it will stick until set otherwise).

This particular case is set up to work with US phone numbers, but starting on line 47 you could easily add in additional country codes and patterns. Just use the US entry as an example. I would also highly recommend adding field validation to your structure field if you’re concerned about the general integrity of the data. Feel free to use and modify this code to fit your needs.

Link: http://learndotcms.com/2012/04/phone-number-formatting-macro/

Image

Phone Number Formatting Macro

Code

## ###############################################################################
## Macro Name: Phone Number Normalizer
## Version: 1.0
## Author: Michael Fienen
## Email: fienen@gmail.com
## Description: Takes a phone number passed to it and attempts to parse it into
## a standard format. Can be used to supplement field validation on a structure.
##
## Required Parameters:
## - $phoneNum = Phone number you want to normalize (string)
##
## Optional Parameters:
## - $phoneCountry = Pass a phone number country code. Only needs to be
## set once unless country changes for different cases. Default: 1 (string)
## - $addCountryCode = Add the $phoneCountry code to the number. Default:
## false (boolean)
## ###############################################################################
#macro(normalizePhone $phoneNum)
   ## SET OPTIONAL VALUE DEFAULTS
   #if(!$UtilMethods.isSet($phoneCountry))
       #set($phoneCountry = '1')
   #end
   #if(!$UtilMethods.isSet($addCountryCode))
       #set($addCountryCode = false)
   #end
 
   ## CREATE THE INTERNATIONAL IDENTIFER HOLDER
   #if($addCountryCode)
       #set($countryCode = "${phoneCountry} ")
   #end
 
   #if($phoneCountry == '1')
       ## REMOVE ALL THE PUNCTUATION WE TEND TO SEE IN NUMBERS
       #set($strippedPhone = $phoneNum.replaceAll('\.|-|\(|\)|\s|\+',''))
       ## REMOVE LEADING +1, IF THERE
       #if($strippedPhone.length() == 11 && $strippedPhone.substring(0,1) == "1")
           #set($strippedPhone = $strippedPhone.substring(1))
       #end
       ## INCLUDE A FAILSAFE JUST IN CASE SOMETHING WENT SCREWY WITH THE NUMBER
       #if($strippedPhone.length() == 10)
           #set($normPhone = "$!{countryCode}(${strippedPhone.substring(0,3)}) ${strippedPhone.substring(3,6)}-${strippedPhone.substring(6)}")
       #else
           #set($normPhone = $phoneNum)
       #end
   ## ADDITIONAL COUNTRY FORMATS CAN FOLLOW HERE
   #else
       #set($normPhone = $phoneNum)
   #end
 
   ## OUTPUT THE NUMBER
   $!{normPhone}
 
   ## RESET OPTIONAL VALUES
   #set($countryCode = '')
#end