First off, I would like to thank Joe Gill for getting me started down the right path. In fact, I'm going to suggest that everyone click that link so you can use his helpful screenshots to navigate where to put the JS I'm about to post.
In any case, I had not seen a full example of parsing a phone number in CRM 2011 as I had with CRM 4.0. Since it is such a common need, I decided to make my own. The following code will strip out all special characters, check and make sure it's a valid set of numbers, and then push it back to the field, parsed properly. This is limited to 10-digit phone numbers at the moment, but the core structure should be easily expanded if you need different types of validation.
The nice thing is, this can point at any entity, and any attribute that holds a phone number. I strongly recommend adding this to your standard JS library for all CRM 2011 projects.
function validatePhone(context)
{
var phone =context.getEventSource().getValue();
var sTmp = phone.replace(/[^0-9]/g, "");
phoneRegex = /^\d{10}$/;
if( !sTmp.match( phoneRegex ) )
{
event.returnValue = false;
alert("Phone must contain 10 numbers.") ;
}
else
{
var sTmpClean = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
context.getEventSource().setValue(sTmpClean);
}
}