CFLib.org – Common Function Library Project

cleanPhone(phoneNum)

Last updated June 24, 2011

Version: 0 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 1 time(s). Average Rating: 5.0

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:

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

Parameters:

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

Full UDF Source:

view plain print about
<cfscript>
/**
 * 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);

}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson