Formatting Phone Numbers in Dynamics CRM

Many Dynamics CRM users get frustrated that when they enter phone numbers into Dynamics CRM, that no formatting is applied to phone number. This not only leads to non-standard phone number formatting–as each user tends to enter phone numbers according to their own preferences–but also requires quite a few additional keystrokes when entering phone numbers.

To eliminate this frustration, we provide this little snipet of Java Scipt to all of our customers. This jscript is placed on the onchange event of each phone number field in CRM. It will take a 7 or 10 digit phone number and automatically format as (323) 776-9987.

Here is the code. Enjoy!

———————————————————–
// Get the field that fired the event.
var oField = event.srcElement;

// Validate the field information.
if (typeof(oField) != “undefined” && oField != null && oField.DataValue != null)
{
// Remove any nonnumeric characters.
var sTmp = oField.DataValue.replace(/[^0-9]/g, “”);

// If the number has a valid length, format the number.
switch (sTmp.length)
{
case “4105551212”.length:
oField.DataValue = “(” + sTmp.substr(0, 3) + “) ” + sTmp.substr(3, 3) + “-” + sTmp.substr(6, 4);
break;

case “5551212”.length:
oField.DataValue = sTmp.substr(0, 3) + “-” + sTmp.substr(3, 4);
break;
}
}