There are a lot of people in the world who prefer keyboard to mouse clicks when it comes to data input. Microsoft Dynamics CRM users usually like and appreciate the user controls that come with the CRM platform, such as the picklist, date picker, lookup, and etc. You may take all the credit as the magic developer for all the cool stuff that MSCRM team has delivered for us. But sooner or later, you may be asked by your CRM users, "Can we do this, can we do that? ", which is often something that MSCRM doesn't work the exact way out-of-box.
One of the features that your CRM users could possible request is, CRM datepicker is a nice control, it works very well, but sometime we as CRM users want to type in the date directly in the textbox instead of using mouse click to pick a date, which is way too slow to get the job done. But the problem is the CRM users often don't know in which format they are supposed to type in the date. You are asked if you can provide a visual hint to the user about the date format. Then as a professional CRM developer, you will quickly realize that, what your CRM users actually want is a watermark for the datepicker field in the CRM form, probably something like the following picture.
You show the idea to your customer, in most cases they will like the idea, and think you are a genius that can read their mind. Then you come to the implementation, which I have done for you.
/**
* Setup watermark for CRM Date fields based on user's date time format settings.
* @author Daniel Cai, http://danielcai.blogspot.com/
*
* Parameters:
* @param dateField: The datetime field that you want to apploy the watermark.
* If not provided, all Date fields in the form will be masked.
*/
watermarkDateField = function(dateField) {
var defaultColor = "#000000";
var watermarkColor = "#9c9c9c";
var watermarkText = USER_DATE_FORMATTED_FORMATSTRING;
var clearWatermark = function(input) {
// Clear the textbox only if the textbox contains the watermark text
if (input.value === watermarkText) {
input.value = "";
input.style.color = defaultColor;
}
};
var updateWatermark = function(input) {
if (input.value === "") {
input.value = watermarkText;
input.style.color = watermarkColor;
}
else {
input.style.color = defaultColor;
}
};
var maskDateField = function(dateField)
{
var inputBox = dateField.childNodes[1].childNodes[0].childNodes[0].childNodes[0];
if (!!inputBox) {
inputBox.attachEvent("onfocus", function() { clearWatermark(inputBox); });
inputBox.attachEvent("onblur", function() { updateWatermark(inputBox); });
inputBox.attachEvent("onchange", function() { updateWatermark(inputBox); });
crmForm.attachEvent("onsave", function() { clearWatermark(inputBox); });
inputBox.title = watermarkText;
updateWatermark(inputBox);
}
};
(function init() {
if (dateField !== undefined) {
maskDateField(dateField);
}
else {
var tables = document.getElementsByTagName("table");
for (var i = 0; i < tables.length; i++) {
if (tables[i].className === "ms-crm-DateTime") {
maskDateField(tables[i]);
}
}
}
})();
};
watermarkDateField();
What you would do is, copy the above code to the onLoad event of your CRM entity form which you want to watermark your date fields, publish the entity change, and open an existing entity record or create a new one, whew, all the empty date fields should have been watermarked!
A couple of notes about the code before we go.
- The dateField parameter of the function is optional. When provided, it will only watermark the provided date field. If dateField is not provided, it will watermark all date fields in the CRM form.
- Using the same technique, you can easily add watermark to any CRM textbox fields if there is ever such need.
Hope this helps.