cleanPhone(phoneNum)
Last updated June 24, 2011
Version: 0 | Requires: ColdFusion 5 | 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:
#cleanPhone("1 312-555-1212 ext 1234")#
</cfoutput>
Parameters:
| Name | Description | Required |
|---|---|---|
| phoneNum | Phone number string to "clean." | Yes |
Full UDF Source:
<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>
Search CFLib.org
Latest Additions
Dave Anderson added
iniToStruct
20 day(s) ago
Dave Anderson added
deDupeArray
20 day(s) ago
Richard added
dice
22 day(s) ago
Isaac Dealey added
getRelative
a while ago
Top Rated
backupDatabase
Rated 5.0, 22 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)
highlightAndCrop
Rated 5.0, 4 time(s)