Data validaition

venkatesh tadip
Tera Contributor

Hi Team,

 

I have a requirement that i want to validate start date is greater that end date, then i need to show an alert message and it should validate all time formats like 12 hour and 24 hour. i want to achieve this in on submit client script.

 

Please let me know your suggestions on this

 

Thanks in advance.

 

 

1 ACCEPTED SOLUTION

@venkatesh tadip 

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
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Arun_Manoj
Mega Sage

Hi @venkatesh tadip ,

 

 

  • Compare Start Date and End Date fields (likely of type Date/Time).

  • Ensure all time formats are correctly interpreted.

  • Show an alert and prevent submission if the condition fails.

 

function onSubmit() {
var startDateStr = g_form.getValue('start_date'); // Replace with your actual field name
var endDateStr = g_form.getValue('end_date'); // Replace with your actual field name

if (!startDateStr || !endDateStr) {
return true;   // Allow form submission if either date is not set
}

var startDate = new Date(startDateStr);
var endDate = new Date(endDateStr);

if (startDate > endDate) {
alert('Start Date must be less than or equal to End Date.');
return false;
}

 

Please mark it as helpful, if the solution is fine.

 

return true;
}

 

Ankur Bawiskar
Tera Patron
Tera Patron

@venkatesh tadip 

you can use this if both are date/time type fields

function onSubmit(){
	var start = g_form.getValue('start_date');
	var end = g_form.getValue('end_date');
	var startDt = new Date(getDateFromFormat(start, g_user_date_time_format)).getTime();
	var endDt = new Date(getDateFromFormat(end, g_user_date_time_format)).getTime();
	if(endDt < startDt){
		alert('End date should be after Start Date');
		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

@venkatesh tadip 

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
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Robert H
Mega Sage

Hello @venkatesh tadip ,

 

You can do that via a UI Policy. Please see the example below. Just replace the names of your start and end date fields accordingly.

 

RobertH_0-1746426137858.png

 

RobertH_1-1746426155117.png

 

function onCondition() {
	g_form.clearValue('work_end');
	g_form.showErrorBox('work_end', 'End date must be after start date');
}

 

Regards,

Robert