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 – 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;
}
}

Comments (11)

  1. jbender says:

    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;

     }

    }

  2. Ben Vollmer says:

    Thanks Jack!!!!

  3. Hubert Hulshof says:

    Ben,

    Could it be that the script – as is – isn’t working in CRM 3.0

  4. Frank Excell says:

    Thats great. I adapted it to New Zealand numbers…thank you. Frank

  5. Paige Cassada says:

    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?!

  6. Paige Cassada says:

    Never mind. I realized what I did. Rookie mistake. Thanks for all the tips!

  7. ck says:

    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;

    }

    }

  8. Michael Ferreira says:

    What about handling extensions?  when you strip out nonnumber characters extensions are lost.   thoughts?

  9. Alan Garcia says:

    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;

    }

    }

  10. 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;

    }

    }

  11. Chad says:

    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?

Skip to main content