The CRM 4.0 SDK had a great example of formatting a phone number for the US. For CRM 2011, that sample isn’t in the SDK that I could find.So after looking at the SDK sample, it looked like it would be pretty easy to upgrade it to CRM 2011. Here is the script modified function FormatPhone(context)
{
// Get the field that fired the event.
var oField = context.getEventSource().getValue();
var sTmp = oField
// Validate the field information.
if (typeof(oField) != "undefined" && oField != null)
{
// Remove any non-numeric characters.
var sTmp = oField.replace(/[^0-9]/g, "");
// If the number has a valid length, format the number.
switch (sTmp.length)
{
case "4105551212".length:
oField = "(" + sTmp.substr(0, 3) + ") " +
sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
break;
case "5551212".length:
oField.DataValue = sTmp.substr(0, 3) + "-" +
sTmp.substr(3, 4);
break;
}
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
When you put the web resource on the field, check the Pass execution context as first parameter.
You now have phone number formatting on each field you have this script on!