NTBuddy

A place to learn about Microsoft SharePoint and Microsoft Dynamics CRM

Formatting Phone Numbers

A common task I routinely  do when setting up any new CRM system is to start the system out and format the phone number into something consistent for my clients.

 I have two different routines for basic installations. There's one I use that will be convered later that will actually use a web service to format the phone number for the proper country. I will start off with something simple. This code is for the USA only as this applies to most of our customers that will use this. This also requires the form to have a coundry field. If one doesn't you can either add one or remove the country checks altogether, which is what I typically do.

 This routine is typically added to the onChange event of each form field so that the field is always properly formatted.

// The first thing we do is pull the country field into a variable

var sCountry = crmForm.all.shipto_country.DataValue;

 

// Make certain that we format the country into the proper format as people tend to put the country in many different ways.

// Convert country to USA

   if (sCountry == null) sCountry = "usa";

   sCountry = sCountry.replace(/us/gi,"usa");

   sCountry = sCountry.replace(/usaa/gi,"usa");

   sCountry = sCountry.replace(/united states/gi,"usa");

   sCountry = sCountry.replace(/united states of america/gi,"usa");

   crmForm.all.shipto_country.DataValue = sCountry.toUpperCase();

 

// Format phone number routine (USA)

function fncFormatPhone(sTemp) {

var sDefaultAreaCode = "219"; // This is the default area code in case one isn't provided

 

if (sCountry.toLowerCase() == "usa") {

sTemp = sTemp.replace(/[^0-9]/g,""); //remove illegeal characters

sTemp = sTemp.substr(0, 14); // First 14 digits

 

if (sTemp.length == 7)             sTemp = "(" + sDefaultAreaCode + ") "   + sTemp.substr(0, 3) + "-" + sTemp.substr(3, 4);

else if (sTemp.length == 8)    sTemp = "(" + sTemp.substr(0, 3) + ") " + sTemp.substr(3, 3) + "-" + sTemp.substr(6, 2);

else if (sTemp.length == 9)    sTemp = "(" + sTemp.substr(0, 3) + ") " + sTemp.substr(3, 3) + "-" + sTemp.substr(6, 3);

else if (sTemp.length == 10)  sTemp = "(" + sTemp.substr(0, 3) + ") " + sTemp.substr(3, 3) + "-" + sTemp.substr(6, 4);

else if (sTemp.length == 11)  sTemp = "(" + sTemp.substr(0, 3) + ") " + sTemp.substr(3, 3) + "-" + sTemp.substr(6, 4) + " ext. 000"  + sTemp.substr(10,  1);

else if (sTemp.length == 12)  sTemp = "(" + sTemp.substr(0, 3) + ") " + sTemp.substr(3, 3) + "-" + sTemp.substr(6, 4) + " ext. 00"    + sTemp.substr(10,  2);

else if (sTemp.length == 13)  sTemp = "(" + sTemp.substr(0, 3) + ") " + sTemp.substr(3, 3) + "-" + sTemp.substr(6, 4) + " ext. 0"      + sTemp.substr(10,  3);

else if (sTemp.length == 14)  sTemp = "(" + sTemp.substr(0, 3) + ") " + sTemp.substr(3, 3) + "-" + sTemp.substr(6, 4) + " ext. "            +  sTemp.substr(10, 4);

 

return sTemp;

} else return sTemp;

}

 

// Now we save the field into the proper format.

/* shipto_fax */

if (crmForm.all.shipto_fax.DataValue != null) crmForm.all.shipto_fax.DataValue = fncFormatPhone(crmForm.all.shipto_fax.DataValue);