Recently in my current project there were multiple date fields validation as per requirement of client. I had to validate them under some given conditions.
I thought of writing this common function for validating multiple date fields by passing parameters. By passing parameters we do not have to write same code for validating multiple date field. suppose we have to validate Start Date, End Date, Estimated End Date, Actual End Date, Deferral Date 1, Deferral Date 2 etc. here is a condition check (Start Date < End date), (End date
Step 1- Get the Schema name and field name of date fields which is to be validate.
Step 2- Create Function-
function ValidateDate(FirstDate, LastDate)
{
var StartDate = Xrm.Page.getAttribute(FirstDate).getValue();
var EndDate = Xrm.Page.getAttribute(LastDate).getValue();
if(StartDate && EndDate)
{
if(StartDate > EndDate)
{
Xrm.Page.ui.setFormNotification("Invalid Date. ", "ERROR");
Xrm.Page.getAttribute(LastDate).setValue(null);
}
}
}
Step 3- Copy and paste the function into Form.js in CRM which is called on form load in CRM.
Step 4- Call the function on date field change event (You can call the function many times as per requirement).
Step 5- Pass the schema name of date field (which is to be validate) as parameter.
Step 6- Click on OK then Save the form and Publish it.