Formatting Telephone Numbers (CRM 2011)

2011-9-21 | apurvghai | Dynamics CRM SDK | C#


This post talks about formatting phone number is US style. The code already available in CRM v4 SDK Guide, however I have re-written the same thing in CRM v2011 model.

It only part of code which is different here in the usage of context class available in XRM pattern.

function validatePhone()


var phone = Xrm.Page.data.entity.attributes.get("telephone1");
var sTmp = phone.getValue().replace(/[^0-9]/g, "");
phoneRegex = /^d{10}$/; 

if(!sTmp.match(phoneRegex))
{
alert("Phone must contain 10 numbers.") ;
}
else
{
switch(sTmp.length)
{
case "4105551212".length:
phone.setValue( "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4));
break;

case "5551212".length:
phone.setValue= sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4);
break;

}

}


References
  1. Formatting Phone Numbers in CRM v4

Recent Posts
Getting started with Dynamics 365 CRM SDK in C#
Jan 14, 19 | Dynamics CRM SDK
Using Postman with Dynamics 365 Online and OnPremise
Jan 14, 19 | Dynamics CRM SDK
Getting Started with WebAPI
Dec 28, 18 | Dynamics CRM SDK