- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago
you can use single onSubmit client script for this
function onSubmit() {
//Type appropriate comment here, and begin script below
g_form.hideErrorBox('u_sdate');
g_form.hideErrorBox('u_edate');
if(g_form.getValue('u_sdate') != '' && g_form.getValue('u_edate')){
var start = new Date(g_form.getValue('u_sdate')).getTime();
var end = new Date(g_form.getValue('u_edate')).getTime();
if(end < start){
var message = 'Please give valid start and end dates';
g_form.showErrorBox('u_sdate', message);
g_form.showErrorBox('u_edate', message);
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
a week ago
You can do this via a UI Policy as well—no need to write a large script.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago
If you use only onChange, then:
You can show the error message immediately when the user selects wrong dates.
BUT it cannot block the form from being submitted — because onChange runs only when a field changes, not at submission time.
So if you rely only on onChange, the user could still click Submit and save the record even if the dates are invalid.
Hope it helps!
Shashank Jain – Software Engineer | Turning issues into insights
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago
Hi @aditya2byte ,
You can easily do it via UI policy.
Please refer this post Solved: Re: Date/time variable validation selected date sh... - ServiceNow Community
In your case you just need to change the condition "start Date is greater than End date"
Thanks,
Bhimashankar H
-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago
you can use single onSubmit client script for this
function onSubmit() {
//Type appropriate comment here, and begin script below
g_form.hideErrorBox('u_sdate');
g_form.hideErrorBox('u_edate');
if(g_form.getValue('u_sdate') != '' && g_form.getValue('u_edate')){
var start = new Date(g_form.getValue('u_sdate')).getTime();
var end = new Date(g_form.getValue('u_edate')).getTime();
if(end < start){
var message = 'Please give valid start and end dates';
g_form.showErrorBox('u_sdate', message);
g_form.showErrorBox('u_edate', message);
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