Format telephone field in CRM 4.0 using javascript onChange event

Here is a simple javascript to format telephone numbers in CRM. This is to be used in the onChange event at the field level. Throw this in there to format telephone numbers using (XXX) XXX-XXXX. Pretty simple code that I found and slightly modified to meet our clients need.

var oField = event.srcElement;
if (typeof(oField) != “undefined” && oField != null)
{
var sTmp = oField.DataValue.replace(/[^0-9]/g, “”);
switch (sTmp.length)
{
case 10:
oField.DataValue = “(” + sTmp.substr(0, 3) + “) ” + sTmp.substr(3, 3) + “-” + sTmp.substr(6, 4);
break;

default:
alert(‘Phone must contain 10 numbers.’);
break;
}
}

I found this code while searching google. It is from Mitch Milam’s blog and the entry is for CRM 3.0 SDK: Phone number format example bug fix. Thanks Mitch!

One Response to “Format telephone field in CRM 4.0 using javascript onChange event”

  1. free2step Says:

    thanks, that was helpful – http://www.free2step.com

Leave a Reply

You must be logged in to post a comment.