Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Formatting Telephone Numbers

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

Telephone number fields in Microsoft Dynamics CRM do not include formatting for telephone numbers. To apply formatting, use a script in the OnChange event of the telephone number field. The following script applies basic formatting used for telephone numbers in the United States. This method supports 7-digit and 10-digit numbers, for example, (410) 555-1212. In the code the expression '"4105551212".length' is equal to 10. Because the code references the event.srcElement, the same code can be used without modification in the OnChange event of any string field that displays a telephone number.

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

// Validate the field information.
if (typeof(oField) != "undefined" && oField != null)
{
 // Remove any non-numeric 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;
 }
}

© 2010 Microsoft Corporation. All rights reserved.


Show:
© 2015 Microsoft