How to validate a date field on Service Portal to enforce minimum days before submission?

barbaratosetto
Tera Contributor

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. 

 

barbaratosetto_0-1753361447736.png

 

barbaratosetto_1-1753361536649.png

 

barbaratosetto_2-1753361549553.png

 

client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) return;

    var ga = new GlideAjax('ValidateRequiredByDate');
    ga.addParam('sysparm_name', 'checkDateWindow');
    ga.addParam('sysparm_required_by', newValue);

    ga.getXMLAnswer(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer === 'invalid') {
            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');
        }
    });
}


script include: 
 
var ValidateRequiredByDate = Class.create();
ValidateRequiredByDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkDateWindow: function() {
        var userDateStr = this.getParameter('sysparm_required_by'); // expected format: yyyy-MM-dd
        var userDate = new GlideDateTime(userDateStr);

        var now = new GlideDateTime();
        now.addDaysLocalTime(7); // 7 dias para frente

        if (userDate.before(now)) {
            return 'invalid'; // return string
        }

        return 'valid'; // return string
    }

});
barbaratosetto_3-1753361633024.png

 

 

 

 

 

1 ACCEPTED SOLUTION

Bhimashankar H
Mega Sage

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

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@barbaratosetto 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

By applying your solution, I'm having the error: "There is a JavaScript error in your browser console"

barbaratosetto_0-1753365753922.png

 

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

barbaratosetto_1-1753365832786.png

 

barbaratosetto_2-1753365849278.png

 

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...  

 

Bhimashankar H
Mega Sage

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!

Bhimashankar H
Mega Sage

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