- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 02:34 AM
How can i restrict to user not to select future dates on a date variable on Service Catalog form
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 02:41 AM
Hi,
no complex script required
Use Catalog UI policy
Start Time [After] Today
function onCondition() {
// valid
alert('Start date cannot be in future');
g_form.showErrorBox('start_date_time','Invalid');
g_form.setMandatory('start_date_time', true);
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 02:39 AM
Hi
Try below and add your field accordingly.
Try this On Change Client Script for your date field that you want to check:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//current date
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);
var startDateNum = getDateFromFormat(newValue, g_user_date_format);
if (startDateNum > currentDateNum) {
alert(“Date is in the future”);
g_form.clearValue("your_date_field");
return false;
}
}
Regards,
Musab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 02:40 AM
Try this onChange Client Script on date field that you want to verify:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//current date
var date= new Date();
var date1= formatDate(date, g_user_date_format);
var date2= getDateFromFormat(date1, g_user_date_format);
var date3= getDateFromFormat(newValue, g_user_date_format);
if (date3 > date2) {
alert(“Date should be in the past”);
return false;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2022 02:41 AM
Hi,
no complex script required
Use Catalog UI policy
Start Time [After] Today
function onCondition() {
// valid
alert('Start date cannot be in future');
g_form.showErrorBox('start_date_time','Invalid');
g_form.setMandatory('start_date_time', true);
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2023 11:14 PM
Hi Ankur
That's a classic, reactive solution but I'm not sure if it really answers on the question.
What if the goal is to actually prevent the future dates from being selectable in the first place (as opposed to letting the user select them just to be alerted that this was not a valid selection) ? IMHO, if the invalid options are not selectable then we are giving the user better experience comparing to alerting them "Wrong date!".
How to achieve that (without fiddling around with the DOM - not a best practice).
//Rafal