Date field Validation to restrict selecting past dates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2020 05:08 AM
Hi Experts,
I have written a onchange catalog client script on the "Start Date" variable to restrict users from not to select the past dates. The below is the code I am using, But the code is also triggering if the selected date is today's date (current date). All I need to is restrict user to not to select any date before current date.
code:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type //get the entered date string
var enteredDate= g_form.getValue('start_date');
//get the current date/time
var rightNow = new Date().valueOf();
var dateToCheck = new Date(enteredDate).valueOf();
if (dateToCheck < rightNow)
{
//Display Error Message
g_form.addErrorMessage(getMessage('Select appropriate date'));
g_form.setValue('start_date','');
}
}
Any ideas on how to achieve this ?
Thanks in advance,
Rocky.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2020 05:55 AM
Just simulated with Variable Set. Works fine.
So is the alert not triggered? The setValue not? Both not? And when strange instance things are happening... cache.do 🙂
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
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
04-13-2020 06:11 AM
The alert and set value both are triggered irrespective of the date I select. Even if select today's date or future date the alert and set value is being triggered.
Thanks,
Rocky.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2020 09:35 AM
Hmm no clue. Same set up works fine here 😞
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
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
04-13-2020 05:10 AM
For example:
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
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
06-20-2022 07:17 AM
But in Agent wworkspace is not reflecting