End Date Validation is not working in Catalog clientscript

thaduri sai
Tera Contributor

Hi Team,

 

We have a catalog client script(Onsubmit) for two variables (start date and End date) - For example we are selecting 25-12-2024 in start date and 10-01-2025 in End date. As per our script it should allow to take the value in end date. But it is throwing an error. Attached screenshot for your reference.

 

While we are taking 25-12-2024 in start date and 26-01-2025 in end date - It is taking working as expected attached screenshots for your reference. Here we have noticed that calculating date number only not month and year.

 

How to fix this bug - Below is the client script(Onsubmit):-

-------------------------------------------------

function onSubmit() {

    var startDate = g_form.getValue('start_date');
    var endDate = g_form.getValue('end_date');

    if (startDate && endDate) {
        if (endDate < startDate) {
            g_form.showFieldMsg('end_date', getMessage('End date should be later than start date'), 'error');

            return false;
        }
    }
    return true;
}
 
Thanks for the adavance.
 
Thanks,
Saikrishna
2 ACCEPTED SOLUTIONS

@thaduri sai 

is it working fine in native?

try this

function onSubmit() {
    //Type appropriate comment here, and begin script below

    g_form.hideFieldMsg('start_date');
    g_form.hideFieldMsg('end_date');

    if (g_form.getValue('start_date') != '' && g_form.getValue('end_date')) {
        var start = new Date(getDateFromFormat(g_form.getValue('start_date'), g_user_date_format));
        var end = new Date(getDateFromFormat(g_form.getValue('end_date'), g_user_date_format));
        if (end.getTime() < start.getTime()) {
            var message = 'Please give valid start and end dates';
            g_form.showFieldMsg('start_date', message, 'error');
            g_form.showFieldMsg('end_date', message, 'error');
            return false;
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

@thaduri sai 

you can have onChange on each of those 2 date variables/field and inform user to select only future date

something like this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    var today = new Date().getTime();
    var selectedDate = new Date(newValue).getTime();
    if (today > selectedDate) {
        alert('You cannot select past date');
        g_form.clear('start_date');
		g_form.setMandatory('start_date', true);
    }

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

14 REPLIES 14

@thaduri sai 

is it working fine in native?

try this

function onSubmit() {
    //Type appropriate comment here, and begin script below

    g_form.hideFieldMsg('start_date');
    g_form.hideFieldMsg('end_date');

    if (g_form.getValue('start_date') != '' && g_form.getValue('end_date')) {
        var start = new Date(getDateFromFormat(g_form.getValue('start_date'), g_user_date_format));
        var end = new Date(getDateFromFormat(g_form.getValue('end_date'), g_user_date_format));
        if (end.getTime() < start.getTime()) {
            var message = 'Please give valid start and end dates';
            g_form.showFieldMsg('start_date', message, 'error');
            g_form.showFieldMsg('end_date', message, 'error');
            return false;
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

Happy new year.

 

Above script is working fine - Thanks for your help man.

 

But we are able to take past start dates and end dates in dates field. We have to take future dates. How to restrict this please?

 

Thanks,

Saikrishna

@thaduri sai 

you can have onChange on each of those 2 date variables/field and inform user to select only future date

something like this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    var today = new Date().getTime();
    var selectedDate = new Date(newValue).getTime();
    if (today > selectedDate) {
        alert('You cannot select past date');
        g_form.clear('start_date');
		g_form.setMandatory('start_date', true);
    }

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@thaduri sai 

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@thaduri sai 

enhance your current script as this

function onSubmit() {

    var startDate = g_form.getValue('start_date');
    var endDate = g_form.getValue('end_date');
    g_form.hideFieldMsg('end_date'); // hide the field message before the validation runs
    if (startDate && endDate) {
        if (endDate < startDate) {
            g_form.showFieldMsg('end_date', getMessage('End date should be later than start date'), 'error');

            return false;
        }
    }
    return true;
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader