How can I restrict a date variable on a catalog item so that users cannot select a date in the past or more than 2 dates in the future?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2019 06:45 AM
I have a catalog item that has two date variables, a delivery date and an expiration date. For both variables, I need users to not be able to select a date that is in the past and also not more than business days in the future.
The script below helped with alerting, but it did not prevent users from being able to select a past date.
// Start Date Variable
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading) {
if(newValue != '') {
//current date
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);
//get start date
var startDateStr = g_form.getValue('sDate');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);
if (startDateNum < currentDateNum) {
alert('You cannot select a date in the past.');
g_form.setValue('sDate', '');
}
}
}
}
// End Date Variable
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading) {
if(newValue != '') {
//current date
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);
//get start date
var startDateStr = g_form.getValue('sDate');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);
//get end date
var endDateStr = g_form.getValue('endD');;
var endDateNum = getDateFromFormat(endDateStr, g_user_date_format);
var diff = endDateNum - startDateNum;
var maxDiff = 30*24*60*60*1000; //30 days * 24 hrs * 60 mins * 60 secs * 1000 ms
if (endDateNum <= 0){
alert('Please use the calendar icon to select a date.');
g_form.setValue('endD', '');
} else if (endDateNum < currentDateNum) {
alert('You cannot select a date in the past.');
g_form.setValue('endD', '');
} else if (endDateNum < startDateNum) {
alert('You cannot select an end date prior to the start date.');
g_form.setValue('endD', '');
}
}
}
}
Thanks!
- Labels:
-
Multiple Versions
-
Request Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2019 07:03 AM
Hello,
This requirement can be easily achieved by using UI policy.
let's assume you have a variable called : delivery date ( delivery_date)
Create UI policy with the following conditions :
delivery_date before today or
delivery_date at or after tomorrow.
Please keep in mind that, you need to create two conditions with " OR "
Then on the Catalog UI Policy Action section, make delivery_date Read Only ( Read Only = true)
Alternatively, you could write client script by selecting Script tab as following to inform users
function onCondition(){
g_form.clearValue() ;
g_form.showFieldMsg('delivery_date','date can not be in the past or more than two days from today', 'error') ;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2019 08:10 AM
Thank you for your response. Adding the first condition that you suggested worked, but I had to change the condition after "or".
delivery_date before today or
delivery_date relative on or after 3 days from now.
That condition prevents users from selecting a past date or a date more than two days in the future.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2019 08:14 AM
Hi there,
Have you considered using a (Catalog) UI Policy to achieve date validations? There's almost no-code needed to achieve date validations this way. Have a look at an article I wrote on this:
No Code date validations thru (Catalog) UI Policies
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2019 08:29 AM
Thank you, very helpful! I've bookmarked your article for future use.
What resolved my issue was updating the condition as shown below:
delivery_date before today or
delivery_date relative on or after 3 days from now.
That condition prevents users from selecting a past date or a date more than two days in the future.