Share via


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

Comments

  • Anonymous
    September 22, 2011
    Thanks for the code,
  • Anonymous
    November 15, 2011
    nice job
  • Anonymous
    May 03, 2012
    I wish this function had been done so that it would work for all phone number fields.
  • Anonymous
    May 08, 2012
    To make it working for all the phone numbers, you will need to rewrite this for different fields or else you can derive a logic of putting the fields into array and run the formatting on save or something.
  • Anonymous
    August 28, 2012
    if We want to formate Number Length grater than 10 what should i do