Validations on list and library Date Time field

Hi Team,
I am using List and Library control with StartDate and Endate time columns. I need to validate Dates should be same. But validation not working here in the dialog box for this control.
Below is the code I am using.

/*****/
function validation(){
fd.field('EndDate').validators.push({
name: 'End Date validation',
error: 'EndDate must be same as StartDate',
validate: function(value) {

                var date1 = fd.field('EndDate').value.toISOString().split('T')[0];
                var date2 = fd.field('StartDate').value.toISOString().split('T')[0];
                console.log('EndDate ' + date1);
                console.log('StartDate ' + date2);
        if (date2 >= date1 || date2 <= date1 ) {
            return false;
        }
        return true;
    }
});
}

/****/
// Calling function when the user changes the date
fd.field('StartDate').$on('change', validation);
fd.field('EndDate').$on('change', validation);
// Calling function on form loading
validation();

image


n

Thanks in advance.

Hello @navpreet.kaur,

What exactly doesn't work? From the screenshot I see that validation triggers for the EndDate field. If you want to trigger validation for the StartDate, you need to add custom valuation to it too.

@mnikitina , yes i need it only for End Date, but in the screenshot, dates are already same the validation does not work in the control dialog box.

@navpreet.kaur,

If you want to compare the date part without the time, you need to use this code:

//clear the time
var date1 = fd.field('EndDate').value.setHours(0,0,0,0);
var date2 = fd.field('StartDate').value.setHours(0,0,0,0);
if (date2 == date1) {
   return true;
}
return false;

@mnikitina
Thank you, it worked.

1 Like