One area that Microsoft CRM 2011 needs assistance in is establishing data input standards and validation. Simple data entry points can quickly start to lose shape if a system administrator doesn’t apply framework to ensure that the application users input data matches the desired requirements. Things like formatting a Phone Number or checking a Zipcode can go a long way to ensuring the data in a system is reliable and presented in a user friendly format, as well as making the data present correctly when used in views and reports.
Because MS CRM 2011 uses JavaScript as its client side scripting language you can rely on Regular Expression Objects (RegExp) to perform these sorts of tasks. I prefer not to restrict a user from entering data, but rather collect the data and if it matches an expression, ensure it is stored in a predetermined format. There are also many things to consider, when determining how to produce the data in the predetermined format. Consider a phone number, in some cases an application user might enter a seven digit number without the area code which would be less then desirable.
Following the North American Numbering Plan (NANP), long distance United States number would start with a 1 as the Country Code followed by the area code, then the prefix and then finally the last 4 digit of the number. So in this example we will assume that the Contacts in the application are primarily from the United States and it is the desire to format the number in the following format. +1 (XXX) XXX-XXXX, this format presents nicely for views and reports.
Phone Number entered into a field without formatting assigned:

The first step to setting up a feature to review a Phone Number that has been entered is to provide a function. The function will test what was entered by the application user, and if it matches the desired expression it will return it formatted. When adding these sorts of functions, it is best to design them to be used universally and not tied to a specific data attribute. In the example below a string variable is passed into the function to be tested against the expression. The method which you store these sorts of scripts in MS CRM 2011 are outside the scope of this Blog discussion, but will ensure we include that topic in a future blog.
function formatPhone(phonenum)
{
var regexObj = /^(?:\+?1[-. ]?)?(?:\(?([0-9]{3})\)?[-. ]?)?([0-9]{3})[-. ]?([0-9]{4})$/;
if (regexObj.test(phonenum))
{
var parts = phonenum.match(regexObj);
var phone = "";
if (parts[1]) { phone += "+1 (" + parts[1] + ") "; }
phone += parts[2] + "-" + parts[3];
return phone;
}
else
{
return phonenum;
}
}
Once you have establish the function you would simply call it from the onChange event of the data attribute you wish to test and format.
function phone_OnChange()
{
if (Xrm.Page.getAttribute("new_phonenumber").getValue() != null)
{
var startphnnum = Xrm.Page.getAttribute("new_phonenumber").getValue();
var finishphnnum = formatPhone(startphnnum);
Xrm.Page.getAttribute("new_phonenumber").setValue(finishphnnum);
}
}
Phone Number that has been passed to the formatting function:

You can enforce the entry of data in a certain format by clearing the value and triggering an alert that warns the application user that the data they entered matched the requirement. Caution should be used if enforcement is deployed. Ensure that you conduct comprehensive testing of your expressions as not to block valid data.
Sample Regular Expression Object string:
Zipcode var regexObj = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
Email var regexObj = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
Note that the Email Regular Expression above determines if the string appears to look like an email, there is a standard that outlines exactly what a constitutes a valid email. That standard is outlined in RFC2822 and to achieve that standard additional development would need to occur on the provided expression.