phoneFormat(varInput, varMask)
Last updated May 9, 2009
Version: 3 | Requires: ColdFusion 5 | 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 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. | Yes |
Full UDF Source:
<cfscript>
/**
* 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
*
* @param varInput Phone number to be formatted. (Required)
* @param varMask Mask to use for formatting. x represents a digit. (Required)
* @return Returns a string.
* @author Derrick Rapley (adrapley@rapleyzone.com)
* @version 3, May 9, 2009
*/
function phoneFormat(varInput, varMask) {
var curPosition = "";
var i = "";
var newFormat = trim(ReReplace(varInput, "[^[:digit:]]", "", "all"));
var 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);
}
</cfscript>
Search CFLib.org
Latest Additions
Shawn Porter added
DeMoronize
3 hour(s) ago
Chris Carey added
readPropertiesFi...
1 day(s) ago
Randy Johnson added
lastDayofWeek
3 day(s) ago
Frank Marion added
sitemapPing
7 day(s) ago
Top Rated
QuickSort
Rated 5.0, 3 time(s)
indentXml
Rated 5.0, 3 time(s)
queryColumnsToSt...
Rated 5.0, 3 time(s)
generateSsccAsn
Rated 5.0, 3 time(s)