- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2025 10:00 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2025 02:02 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2025 10:26 PM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2025 10:51 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2025 02:02 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2025 11:24 PM
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.
function onCondition() {
g_form.clearValue('work_end');
g_form.showErrorBox('work_end', 'End date must be after start date');
}
Regards,
Robert