Formatting Telephone Numbers (CRM 2011)

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