CFLib.org – Common Function Library Project

phoneFormat(varInput[, varMask])

Last updated February 11, 2011

author

Derrick Rapley

Version: 4 | Requires: CF5 | Library: StrLib

Description:
phoneFormat() takes a 10 digit phone number in any format and returns a value in the specified mask. Here are some mask examples, (xxx) xxx-xxxx, xxx.xxx.xxxx, xxx xxx-xxxx, etc. The only requirement is that numeric values in the mask must be represneted by an "x."

Return Values:
Returns a string.

Example:

<cfset number1="3019876352">
<cfset number2="(301) 987-6352">
<cfset number3="301.987.6352">
<cfoutput>
#number1# = #phoneFormat(number1, "(xxx) xxx-xxxx")#<br>
#number2# = #phoneFormat(number2, "xxx.xxx.xxxx")#<br>
#number3# = #phoneFormat(number3, "xxx xxx-xxxx")#<br>
</cfoutput>

Parameters:

Name Description Required
varInput Phone number to be formatted. Yes
varMask Mask to use for formatting. x represents a digit. Defaults to (xxx) xxx-xxxx No

Full UDF Source:

/**
 * Allows you to specify the mask you want added to your phone number.
 * v2 - code optimized by Ray Camden
 * v3.01 
 * v3.02 added code for single digit phone numbers from John Whish   
 * v4 make a default format - by James Moberg
 * 
 * @param varInput 	 Phone number to be formatted. (Required)
 * @param varMask 	 Mask to use for formatting. x represents a digit. Defaults to (xxx) xxx-xxxx (Optional)
 * @return Returns a string. 
 * @author Derrick Rapley (adrapley@rapleyzone.com) 
 * @version 4, February 11, 2011 
 */
function phoneFormat(varInput) {
       var curPosition = "";
       var i = "";
       var varMask = "(xxx) xxx-xxxx";
       var newFormat = "";
       var startpattern = "";
   if (arrayLen(arguments) gte 2){ varMask = arguments[2]; }
       newFormat = trim(ReReplace(varInput, "[^[:digit:]]", "", "all"));
       startpattern = ReReplace(ListFirst(varMask, "- "), "[^x]", "", "all");
       if (Len(newFormat) gte Len(startpattern)) {
               varInput = trim(varInput);
               newFormat = " " & reReplace(varInput,"[^[:digit:]]","","all");
               newFormat = reverse(newFormat);
               varmask = reverse(varmask);
               for (i=1; i lte len(trim(varmask)); i=i+1) {
                       curPosition = mid(varMask,i,1);
                       if(curPosition neq "x") newFormat = insert(curPosition,newFormat, i-1) & " ";
               }
               newFormat = reverse(newFormat);
               varmask = reverse(varmask);
       }
       return trim(newFormat);
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson