Thank you for the sample, that gets me close. However there are a couple issues with your demo.
- The onChange event fires before the listener does, so the code in the listener doesn't actually do anything. I added console.log messages to display it.
- The reason the onChange event code works is due to the fact that the length variable is undefined first time through. Once a valid date has been entered, any future runs won't actually do the 2 digit year processing in the onChange event.
- Because of 1 & 2, if the user enters a 4 digit year that is in the future (2030) the picker will change the date to 1930.
- The listener code that checks for length fails when there is a previous value and the user manually deletes it, as the split array has no element 3.
- The fix I added for #2 is irrelevant due to #1.
I can't find a way to intercept the value that the user entered, before the picker component changes the year to a 4 digit. Without that, I believe that there's really no way of being able to implement this correctly.
I modified the demo code you provided to show this happening. I'm running in Chrome browser, using the debugger tools (F12).
var inputLength;
document.getElementById("datepicker").addEventListener("change", function () {
var dateObj = document.getElementById("datepicker").ej2_instances[0];
console.log("listener: " + dateObj.element.value); // this needs to happen prior to onChange event
// manual delete of the data, then tabbing out causes error to toss here, 3rd element in array doesn't exist
//inputLength = dateObj.element.value.split("/")[2].length; // This operation based on the default date format "MM/dd/yyyy"
var valSplit = dateObj.element.value.split("/");
inputLength = valSplit.length === 3 ? valSplit[2].length : 0;
});
function onDatechange(args) {
var datepicker = document.getElementById("datepicker").ej2_instances[0]; // Get the datepicker instance
var currentYear = new Date().getFullYear();
console.log("event: " + datepicker.element.value); // DataChange occurs before Listener does
if ((args.value) && (inputLength != 4)) {
if (args.value.getFullYear() > currentYear) {
let year = args.value.getFullYear();
let month = args.value.getMonth();
let day = args.value.getDate();
datepicker.value = new Date(year - 100, month, day);
}
}
else {
datepicker.value = args.value;
}
}
var inputLength;
document.getElementById("datepicker").addEventListener("change", function () {
var dateObj = document.getElementById("datepicker").ej2_instances[0];
console.log("listener: " + dateObj.element.value); // this needs to happen prior to onChange event
// manual delete of the data, then tabbing out causes error to toss here, 3rd element in array doesn't exist
//inputLength = dateObj.element.value.split("/")[2].length; // This operation based on the default date format "MM/dd/yyyy"
var valSplit = dateObj.element.value.split("/");
inputLength = valSplit.length === 3 ? valSplit[2].length : 0;
});
function onDatechange(args) {
var datepicker = document.getElementById("datepicker").ej2_instances[0]; // Get the datepicker instance
var currentYear = new Date().getFullYear();
console.log("event: " + datepicker.element.value); // DataChange occurs before Listener does
if ((args.value) && (inputLength != 4)) {
if (args.value.getFullYear() > currentYear) {
let year = args.value.getFullYear();
let month = args.value.getMonth();
let day = args.value.getDate();
datepicker.value = new Date(year - 100, month, day);
}
}
else {
datepicker.value = args.value;
}
}