Here is the script for formatting the phone numbers in Microsoft CRM. In this example, new_contact phone is a custom field, but it can work on any Field...

Field Events - OnChange

The OnChange event is fired when the data in a form field has changed and focus is lost. The Microsoft CRM 3.0 application processes the OnChange event first and the data in the field is validated again following the execution of your OnChange event code.

This event is supported for following data types:

  • Text
  • Picklist
  • Boolean
  • Float
  • Integer
  • Money
  • Date Time
  • Lookup
  • Status

Example

The following code example shows how to format basic U.S. phone numbers. This method supports 7-digit and 10-digit numbers, for example, (410) 555-1212.

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

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