Monday, June 14, 2010

CRM 4.0 Formatting Form Fields

The following is an intuitive Mask/UnMask JavaScript object. If you’re looking to toggle other clients (other than an entity form) you might use it as a template for a server side plug-in. The object makes it easy to enforce various types of formatting such as phone numbers , a social security number, credit card numbers and many more. The sample below formats the account Main and Other phone fields.

When a user gives focus to a phone field the object unmasks the field’s value leaving only numbers (the original user input) or blank. Once focus is lost the mask is applied again, if a value exists, giving it its final display. The object also fires when the form is saved to support a situation where focus is not fired i.e. when the user uses the keyboard to save the record.

There’re also other features such as displaying the format as a title or telling the mask to fire when the form loads. The later is hardly required but you might find it useful to leverage the user interaction with the form … and cleans/reformat imported/external values along the way.

If you take a closer look you’ll notice that the object receives both a format (mask) such as “(##) ###-####” and a regular expression which is an unmask filter e.g. “\\D”. “\\D” means anything but numbers which is what you need to remove/replace to get the user’s original input.

The code goes in the account entity onload event.
Give it a go and feel free to leave comments.

function Mask(format)
var m = this;

/* e.g. (##) ###-#### */
m.Format = format;
m.Field = null;
/* OnLoad - Might be Used to cleans imported values. */
m.OnLoad = false;
A regular expression for unwanted (bad) characters.
e.g. A field (e.g. phone number) original (before formatting) characters must contain numbers only.
m.Filter = null;

var onloadbound = false;
var onsavebound = false;
var onfocusbound = false;

m.Mask = function()
if (!m.Field)
return alert("Mask is missing a field");

/* Sets the textbox title to required Format */
m.Field.title = m.Format;

if (!onfocusbound)
onfocusbound = m.Field.attachEvent("onfocusin", unmask);
m.Field.attachEvent("onfocusout", mask);

if (m.OnLoad && !onloadbound)
onloadbound = mask() == undefined;

if (!onsavebound)
onsavebound = crmForm.attachEvent("onsave",mask);

function unmask()
m.Field.DataValue = strip();

function mask()
/* Reformat */
var formated = m.Format;
/* value as Array of characters */

var splitValue = strip().split("");

if (splitValue.length == 0)
m.Field.DataValue = null;

/* Regex defining a single placeholder */
var placeHolderReg = new RegExp("#{1}");
/* Replace each placeholder with a single character */
for(var i = 0; i < splitValue.length ; i++)
formated = formated.replace(placeHolderReg,splitValue[i]);

m.Field.DataValue = formated;

function strip()
if (!m.Field.DataValue)
return "";
/* Strip field from unwanted characters */
var valueOnlyReg = new RegExp(m.Filter,"gi");
return m.Field.DataValue.replace(valueOnlyReg,"");

function OnCrmPageLoad()
var phoneMask = new Mask("(##)###-####");
phoneMask.Field = crmForm.all.telephone1;
phoneMask.Filter = "\\D"; //Unmask Filter - Strip Everything which is not a Number

var cellMask = new Mask("+(##) ### ####");
cellMask.Field = crmForm.all.telephone2;
phoneMask.OnLoad = true;
cellMask.Filter = "\\D"; //Unmask Filter - Strip Everything which is not a Number



Andriy a33ik Butenko said...

Welcome back, Adi!
I missed your posts a lot!

Anonymous said...

Thanks for the post! it will be very useful to me xD

Taylor said...

I have an attribute which show as a ‘bit’ field in a CRM 4.0 database. I need to dynamically add it to a dynamic entity then set its value to either 0 or 1. Which is the best way of doing this?

vinod said...

Thanks for sharing a nice article....

Ray Business Technologies,, is a leading IT Services Provider for Microsoft Dynamics CRM implementations. Raybiztech has worked with clients across all business segments to implement Microsoft Dynamics CRM. Whether you are deploying CRM for the first time or you have many years of experience, Raybiztech’s unique insights can help you achieve your objectives. Raybiztech has implemented Microsoft Dynamics CRM projects for a number of small and medium sized businesses on time. Raybiztech is Microsoft Silver Partner and have deep expertise on Microsoft CRM implementations, Microsoft CRM customization, and CRM upgrade. We have deep expertise on CRM 4.0, CRM 2011 and Online CRM.

naher88 said...

This is a good article & good site.Thank you for sharing this article. It is help us following categorize:
digital supply chain management, Delivering high-quality service for your business applications,
Solutions for all Industries,
Getting your applications talking is the key to better business processes,
Rapid web services solutions for real business problems,
Understanding Your Data is the Key to Good Decision-Making,
Web-based Corporate Document Management System,
Outsourcing Solution,
Financial and Operations Business Intelligence Solution,


ERP said...

ERPTREE is a leading oracle fuison HCM Training Institute. we offer this course through online we have great
experience in succeeding students through online courses. we can calculate our performance through their honest
comments in our sites in supporting our services. we have referral program so candidates can earn money through
referral. you can share your live experience with other can generate you some money.

Oracle Fusion HCM Online Training