- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2025 05:54 AM
Hi everyone,
I'm working on a Catalog Item that includes a date variable called Required By, and I need to validate that the selected date is at least 7 days in the future from the current date.
This validation should happen on the Service Portal, when the user selects a date, and should prevent submission if the condition isn't met.
So far I’ve created a Script Include and one client script (see below). I've also tried a UI Policy, but it didn't work.
Note: I have a few client scripts in the same catalog item to manage other fields - none related to the 'required_by' which I'm trying to apply the mentioned rule.



client script:

Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2025 10:08 PM
Hi @barbaratosetto ,
I hope you saw my reply.
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. It will help future readers as well having similar kind of questions and close the thread.
Thanks,
Bhimashankar H
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2025 06:23 AM
you can use this catalog client script and no GlideAjax required
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue)
        return;
    var selectedDate = new Date(getDateFromFormat(g_form.getValue('required_by'), g_user_date_format));
    var sevenDaysFromNow = new Date();
    sevenDaysFromNow.setDate(today.getDate() + 7);
    if (selectedDate.getTime() < sevenDaysFromNow.getTime()) {
        g_form.showFieldMsg('required_by', 'Please select a date at least 7 days after today.', 'error');
        g_form.clearValue('required_by');
    } else {
        g_form.hideFieldMsg('required_by');
    }
}If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2025 07:05 AM
By applying your solution, I'm having the error: "There is a JavaScript error in your browser console"

I just figured out that there was an error in my UI Policy. I got it corrected and it's finally working.


I'd prefer using a client script since it's a more robust approach, but I don't know how to fix this error - tried AI help, but still...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2025 08:12 AM
Hi @barbaratosetto ,
To ensure users can only select a date at least 7 days after today in ServiceNow, you can use a client-side onChange script. You don't need to call the glideAjax for simple date validate.
Example: Client Script for Date Validation
Type: Client Script
UI Type: All (or pick Workspace if needed)
Script Name: Validate Minimum Date
Applies to: The date field you want to control (for example, requested_date)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
        return;
    }
    // Get today's date (ignore time)
    var today = new Date();
    today.setHours(0,0,0,0);
    // Parse the selected date
    var selectedDate = new Date(newValue);
    selectedDate.setHours(0,0,0,0);
    // Calculate minimum allowed date (today + 7 days)
    var minDate = new Date(today);
    minDate.setDate(today.getDate() + 7);
    // Validation
    if (selectedDate < minDate) {
        g_form.showFieldMsg('your_date_field_name', 'Please select a date at least 7 days from today (' + minDate.toISOString().substring(0,10) + ').', 'error');
        g_form.setValue('your_date_field_name', '');
    } else {
        g_form.hideFieldMsg('your_date_field_name', true); // Remove any previous error
    }
}
- This script clears the date and shows a message if the selected date is not at least 7 days after today (e.g., if today is 2025-07-24, the user must pick July 31, 2025 or later). 
Thanks,
Bhimashankar H
-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2025 10:08 PM
Hi @barbaratosetto ,
I hope you saw my reply.
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. It will help future readers as well having similar kind of questions and close the thread.
Thanks,
Bhimashankar H
