CFLib.org – Common Function Library Project

cleanPhone(phoneNum)

Last updated June 24, 2011

author

Jeff Horne

Version: 0 | Requires: CF5 | Library: StrLib

Description:
Cleans up phone numbers that include leading 1s, text before and extension number or other characters. Returns in (###) ###-#### x### format. Uses PhoneFormat originally submitted by Derrick Rapley.

Return Values:
Returns a string.

Example:

<cfoutput>
	#cleanPhone("1 312-555-1212 ext 1234")#
</cfoutput>

Parameters:

Name Description Required
phoneNum Phone number string to "clean." Yes

Full UDF Source:

/**
 * Strips unnecessary characters from phone numbers and returns a consistent looking phone number and extension, if necessary.
 * 
 * @param phoneNum 	 Phone number string to "clean." (Required)
 * @return Returns a string. 
 * @author Jeff Horne (jeff.horne@trizetto.com) 
 * @version 0, June 24, 2011 
 */
function cleanPhone(PhoneNum) {
	var thisCleanPhone ="";

	PhoneNum = ReReplace(trim(PhoneNum), "[^[:digit:]]", "", "all");
	
	// Trim away leading 1 in phone numbers.  No area codes start with 1 
	
	if (Left(Trim(PhoneNum),1) eq 1) {
      PhoneNum = Replace(PhoneNum,'1','');
	}

	thisCleanPhone = PhoneFormat(Left(PhoneNum,10));
	
	// if phone number is greater that 10 digits, use remaining digits as an extension
	
	if (len(trim(PhoneNum)) gt 10) {
		thisCleanPhone = thisCleanPhone &" x"& Right(PhoneNum,(len(trim(PhoneNum))-10));	
	}
	
	return trim(thisCleanPhone);

}
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