RE: Single line of text to Date field
Hi James,
You can use below javascript to validate the date value in text field. Also you can restrict the text field length to 10.
You may also need to implement same validation at server side by registering plug-in steps on create of record and on update of text this field to avoid setting invalid date value from other sources like data import and integration.
function isValidDate(dateString) {
if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))
return false;
var parts = dateString.split("/");
var day = parseInt(parts[1], 10);
var month = parseInt(parts[0], 10);
var year = parseInt(parts[2], 10);
if(year < 1000 || year > 3000 || month == 0 || month > 12)
return false;
var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
monthLength[1] = 29;
return day > 0 && day <= monthLength[month - 1];
}
function onDateFieldChange() {
var dateString = Xrm.Page.getAttribute("TextFieldName").getValue();
if(dateString != null && !isValidDate(dateString))
Xrm.Page.getControl("TextFieldName").setNotification("Please enter valid date with MM/DD/YYYY format.","100");
else
Xrm.Page.getControl("TextFieldName").clearNotification("100");
}