start date and end date validation

GBS
Tera Contributor

I have two fields in a delegate form starts and ends, when the start date and end date are equal and when the end date is less than the start date it should show an error message. I'm using the below onchange client script, the script is working only when the end date is less than the start date but it is not showing any error when the start date and end are the same.

 

GBSaiKiran_0-1717688021827.png

 

 

Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    var selectedStartDate = g_form.getValue("starts");
    if (isLoading || newValue === '' || selectedStartDate === '') {
        return;
    }

    var selectedEndDate = new Date(newValue).getTime();
    if (new Date(selectedStartDate) > selectedEndDate) {
        g_form.setValue("ends", ' ');
        g_form.showFieldMsg('ends', "The End Date should not be before the Start Date.", 'error');
    } else if(new Date(selectedStartDate) === selectedEndDate){
        g_form.setValue("ends", ' ');
        g_form.showFieldMsg('ends', "The End Date should not be same as the Start Date.", 'error');
    }


}
1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

Since these are date/time fields, the values pictured aren't equal.  I would try the low-code scripted UI Policy approach:

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...