Validation for start date not working as expected
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 10:03 AM
Hi ,
I have a record producer and have field as start date of Type date . I wanted to add a validation as we cannot select past date . It should only allow to select present date and future dates .
I have written below catalog client script for it :
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 10:06 AM
Hi Jaya ,
i have corrected the code with some changes please try the below:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var selectedDate = new Date(g_form.getValue('license_start_date'));
var today = new Date();
today.setHours(0, 0, 0, 0);
if (selectedDate < today) {
g_form.addErrorMessage("License start date cannot be in the past.");
g_form.clearValue('license_start_date');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 10:22 AM
Hello @Jaya9
In your code you are comparing input as date and comparing with datetime so it's not working as expected.
Also, when script is not working do try to put alert statement for logging so you know what is happening.
Also, this part can be easily achieved through ui policy without writing any script as ServiceNow is more of nocode lowcode.
Refer this best article which will help you https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...
If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.
Thanks & Regards
Viraj Hudlikar.