Fun with JScript and OnLoad - Phone Numbers...

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 - OnChangeThe 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 ExampleThe 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;
  }
}

Comments

  • Anonymous
    October 13, 2006
    Below is a better version of this script.  It does not error out if your clear a value from the field and move to another... // Get the field that fired the event. var oField = event.srcElement; // Validate the field information. if (oField.DataValue != "undefined" && 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;  } }

  • Anonymous
    October 13, 2006
    Thanks Jack!!!!

  • Anonymous
    April 12, 2007
    Ben, Could it be that the script - as is - isn't working in CRM 3.0

  • Anonymous
    May 05, 2007
    Thats great. I adapted it to New Zealand numbers...thank you. Frank

  • Anonymous
    July 09, 2007
    I love this script but it does not seem to be working when I delete the phone number and try to re-add it. Am I missing something?!

  • Anonymous
    July 11, 2007
    Never mind. I realized what I did. Rookie mistake. Thanks for all the tips!

  • Anonymous
    November 09, 2007
    Updated for 11 digit numbers 1(800)555-1212 // Get the field that fired the event. var oField = event.srcElement; // Validate the field information. if (oField.DataValue != "undefined" && 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 "14105551212".length:     oField.DataValue = sTmp.substr(0, 1) + "("  + sTmp.substr(1, 3) + ") " + sTmp.substr(4, 3) + "-" + sTmp.substr(7, 4);     break;   case "5551212".length:     oField.DataValue = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4);     break; } }

  • Anonymous
    December 19, 2007
    The comment has been removed

  • Anonymous
    August 11, 2008
    Below is the same script with cases added for extensions up to a 5 digit extension set: var oField = event.srcElement; // Validate the field information. if (oField.DataValue != "undefined" && 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;   case "41055512121".length:     oField.DataValue = "(" + sTmp.substr(0,3) + ")" + sTmp.substr(3,3) + "-" + sTmp.substr(6,4) + " ext." + sTmp.substr(10,1);     break; case "410555121212".length:     oField.DataValue = "(" + sTmp.substr(0,3) + ")" + sTmp.substr(3,3) + "-" + sTmp.substr(6,4) + " ext." + sTmp.substr(10,2);     break; case "4105551212123".length:     oField.DataValue = "(" + sTmp.substr(0,3) + ")" + sTmp.substr(3,3) + "-" + sTmp.substr(6,4) + " ext." + sTmp.substr(10,3);     break; case "41055512121234".length:     oField.DataValue = "(" + sTmp.substr(0,3) + ")" + sTmp.substr(3,3) + "-" + sTmp.substr(6,4) + " ext." + sTmp.substr(10,4);     break; case "410555121212345".length:     oField.DataValue = "(" + sTmp.substr(0,3) + ")" + sTmp.substr(3,3) + "-" + sTmp.substr(6,4) + " ext." + sTmp.substr(10,5);     break; } }

  • Anonymous
    February 03, 2009
    Thanks for this, it's great!  I did tweak it a bit.  A couple of places there was not a space after the ")" it jammed the number together.  I made it ") " in three places, I think.  Here it is: var oField = event.srcElement; // Validate the field information. if (oField.DataValue != "undefined" && 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;  case "41055512121".length:    oField.DataValue = "(" + sTmp.substr(0,3) + ") " + sTmp.substr(3,3) + "-" + sTmp.substr(6,4) + " ext." + sTmp.substr(10,1);    break; case "410555121212".length:    oField.DataValue = "(" + sTmp.substr(0,3) + ") " + sTmp.substr(3,3) + "-" + sTmp.substr(6,4) + " ext." + sTmp.substr(10,2);    break; case "4105551212123".length:    oField.DataValue = "(" + sTmp.substr(0,3) + ") " + sTmp.substr(3,3) + "-" + sTmp.substr(6,4) + " ext." + sTmp.substr(10,3);    break; case "41055512121234".length:    oField.DataValue = "(" + sTmp.substr(0,3) + ") " + sTmp.substr(3,3) + "-" + sTmp.substr(6,4) + " ext." + sTmp.substr(10,4);    break; case "410555121212345".length:    oField.DataValue = "(" + sTmp.substr(0,3) + ") " + sTmp.substr(3,3) + "-" + sTmp.substr(6,4) + " ext." + sTmp.substr(10,5);    break; } }

  • Anonymous
    March 19, 2010
    The code that includes formatting for an phone with an extension is great, and is almost exactly what I need.  The only thing I need to change is that instead of formatting 7 digit numbers, I need it to prompt the user with an alert that 10 or more digits are required.  I'm having some trouble replacing the 7 digit case with such an alert... does anyone have a suggestion that might help me out?