Set error message if the start date is greater than end date.

Shaik22
Tera Expert

Set error message if the start date is greater than end date.Please help.

7 REPLIES 7

Shakeel Shaik
Giga Sage
Giga Sage

hi @Saidu 

 

Here is the sample script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var currentDate = new Date(); //Current Date
    var selectedDate = new Date(g_form.getValue('u_date_field')); //Date field value

    if (currentDate.getTime() >= selectedDate.getTime()) {
        //Past Date on Date field should thrown an error near field
        g_form.addErrorMessage('You can not select a Past Date');
        return false;
    }

}

 

Please check and let me know

Thanks 🙂

Thanks,
Shakeel Shaik 🙂

 

Steps:

1. Create OnChange Client script on start date field.

2. scrip:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var endDate = new Date(); //Current Date
    var startDate = new Date(g_form.getValue('u_start_date')); //Change value as per requirement.

    if (endDate.getTime() > startDate.getTime()) {
        
        g_form.addErrorMessage('You can not select a Future Date');
        return false;
    }

}

 

Please check and Let us know.

 

Thanks 🙂

Thanks,
Shakeel Shaik 🙂

Sebastian L
Mega Sage

Hi, you can take inspiration from this ootb script on Change: 

 

function onSubmit() {
    var startDate = g_form.getValue("start_date");
    var endDate = g_form.getValue("end_date");
    var format = g_user_date_time_format;

    if (startDate === "" || endDate === "")
        return true;

    // get date strings into a number of milliseconds since 1970-01-01
    var startDateMs = getDateFromFormat(startDate, format);
	var endDateMs = getDateFromFormat(endDate, format);
	
	if (startDateMs < endDateMs)
		return true;

	g_form.clearMessages();
	
	// 0 from "getDateFormat" means an invalid date string was passed to it
	if (startDateMs === 0 || endDateMs === 0) {
        if (startDate === 0)
            g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("start_date")));

        if (endDate === 0)
            g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("end_date")));

        return false;
    }

	if (startDateMs > endDateMs) {
        g_form.addErrorMessage(new GwtMessage().getMessage("{0} must be after {1}", g_form.getLabelOf("end_date"), g_form.getLabelOf("start_date")));

        return false;

    }
}

Best regards,
Sebastian Laursen

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can simply use onChange client script OR onSubmit OR UI policy for this

onSubmit

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

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

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

	}
}

Regards
Ankur

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