MS CRM 2011 Field Formatting and Regular Expression Objects with JavaScript

by Webfortis 7. June 2012 13:22

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. 

Tags: , , , , ,

Categories: Dynamics CRM | JavaScript

Comments (4) -

6/9/2012 1:06:56 AM #

-

Article was very well written!I recommend this article.

- |

6/14/2012 10:03:48 AM #

SpencerAtlanta

Hi again Webfortis:

Many sincere thanks to you, for your support blog and YouTube videos  --  it worked beautifully for standard American telephone format, ie:  (111) 222-3333  ...  

The issue we are trying to resolve:   Importing pre-formatted contacts from Outlook 2010 and/or entering American telephone numbers with a business extensions, ie:   (444) 555-6666 x7777777  ...   perfect example:  Microsoft contacts have American 10-digit numbers and 7-digit extensions.

More specifically, (444) 555-6666 x7777777 with a single space between the 10-digit number and 'x', no space between 'x' and 7-digit extension.  

Sorry I am not an experienced java-script-code professional, but I can certainly cut/paste and follow the process you have on YouTube  -- Any suggestions?

Thank you again,

Scott

SpencerAtlanta |

6/20/2012 3:03:08 PM #

Chuck Oldes

I would recommend that the extention number be handled as a seperate attribute. This is a cleaner way of completing that requirement, which later can be leverage with a telephony interface. Also keep in mind that ANI/caller id does not pass the extention so matching a number for lookup when a caller calls in may become difficult.

Chuck Oldes |

7/19/2012 1:04:19 PM #

SimpleScripts

Great post. I was checking continuously this blog and I’m impressed! Extremely helpful info specially the last part I care for such information a lot. I was seeking this certain information for a very long time. Thank you and best of luck.

SimpleScripts |

Pingbacks and trackbacks (1)+

Comments are closed

Month List