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

@Saidu 

Thank you for marking my response as helpful.

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

Regards
Ankur

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

@Shaik

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

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

Service_RNow
Mega Sage

HI

Create an onChange script for your start_date and end_date fields.

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

   //Type appropriate comment here, and begin script below
    var nowDate = new Date();				//Creates a JS date object for right now
    var checkStart = new Date(newValue);	// Create a JS Date object using the desired start_date field (from newValue parameter)
       
    if (nowDate >= checkStart){             //JS Date objects compared
	   alert('You cannot select today or a day in the past');
	   g_form.setValue('start_date','');
   }
}
//end_date onChange Function
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Type appropriate comment here, and begin script below
    var startDate = new Date(g_form.getValue('start_date'));   //get the value of the start_date field using the g_form.getValue method
    var checkEnd = new Date(newValue);	                       // create a new JS date from the desired end_date field (using newValue parameter)
       
    if (checkEnd <= startDate){                                //Again - a simple JS Date object comparison
	   alert('End date must be after start date');
	   g_form.setValue('end_date','');
   }
}

Please mark reply as Helpful/Correct, if applicable. Thanks!