Format telephone field in CRM 4.0 using javascript onChange event August 20, 2008
Posted by Joel Kriel in CRM 4.0 & .Net development.Tags: crm 4.0, crm 4.0 onchange javascript, crm telephone format, javascript try catch
1 comment so far
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!