Date should not be in past
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 09:59 PM
Hi All,
As per my requirement Date should not be selected as past if select we should want to clear the date.
If I do with UI Policy with Run Script I'm getting but I should want get with On change client script.
Please let us know how can i get this requirement with out using ui policy for this
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 10:16 PM
HI you can use ui policy check below video to achieve your requirement

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 10:22 PM
Sirri already mentioned having achieved that 😉
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
01-18-2024 10:41 PM - edited 01-18-2024 10:42 PM
Hello @Sirri ,
As @Mark Roethof wisely recommended, sticking with UI policies is often preferred for their simplicity and alignment with ServiceNow's low-code philosophy. If you find that UI policies meet your needs seamlessly, they are a robust choice.
However, I understand your curiosity about alternative solutions. For your specific requirement, an onChange client script can be employed. Below is a sample script to prevent the selection of past dates:
function onChangeDate(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '')
return;
var currentDate = new Date().getTime();
var selectedDate = new Date(g_form.getValue('yourDateField')).getTime();
if (selectedDate < currentDate) {
var errorMessage = 'Please choose a date equal to or after today.';
g_form.clearValue('yourDateField');
g_form.addErrorMessage(errorMessage);
}
}
Additionally, if you're interested in exploring a comprehensive article on No-Code date validations through (Catalog) UI Policies, I recommend checking out this enlightening and well-explained piece shared by @Mark Roethof : No Code date validations through (Catalog) UI Policies
And below are the few of YouTube links as well,
- How to restrict past/future date selection in #servicenow
- A Date field Should not allow Past Dates using Client Script in ServiceNow
Let me know your views on this and Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Aniket
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2024 05:43 AM
Hi @Aniket Chavan ,
I have tried with this but it not taking current date.
when ever select current date it will showing as error.
but my requirement is we cannot select past date only but we can select current date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2024 02:47 AM
Hello @Sirri ,
Please give a try to the script below and see how it works for you and I have tested it from my end and its working as expected for me even if I select the current date it's not throwing an error and it will clear the value of the variable and show the error message only if the selected date is past date.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var currentDate = new Date().setHours(0, 0, 0, 0); // Set time to midnight for accurate comparison
var selectedDate = new Date(g_form.getValue('yourDateField')).setHours(0, 0, 0, 0);
if (selectedDate < currentDate) {
var errorMessage = 'Please choose a date equal to or after today.';
g_form.clearValue('yourDateField');
g_form.addErrorMessage(errorMessage);
}
}
Let me know your views on this and Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Aniket