The CreatorCon Call for Content is officially open! Get started here.

client script is not working in the catalog item

chandan2212
Tera Contributor

Hi All, 

 

user should not select thedate  before 90 days from the current date 

for example : today date is 15 oct 2025 than user should not select before 17 july or 16 july but he can select 10 aug 1 sept or future date also . this script is not working in the catalog item , Can anyone help me :

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var selectedDate = new Date(newValue);
    var ninetyDaysAgo = new Date();
    ninetyDaysAgo.setDate(ninetyDaysAgo.getDate() - 90); // Calculate date 90 days ago

    // Set hours, minutes, seconds, and milliseconds to 0 for accurate comparison
    selectedDate.setHours(0, 0, 0, 0);
    ninetyDaysAgo.setHours(0, 0, 0, 0);

    if (selectedDate < ninetyDaysAgo) {
        g_form.addErrorMessage('Date must be within the last 90 days.');
        g_form.setValue('deletion_date', '');
    } else {
        alert('abc')
    }
}
 
Thanks and regards,
Chandan 
1 ACCEPTED SOLUTION

Kieran Anson
Kilo Patron

I'd recommend switching to the no-code approach as documented by @Mark Roethof in the following article:

No Code date validations through (Catalog) UI Policies - ServiceNow Community

View solution in original post

3 REPLIES 3

Rafael Batistot
Kilo Patron

Hi @chandan2212 

 

In Service Catalog, the newValue for a date field is not always in a format (yyyy-MM-dd) that new Date() can parse correctly in all browsers or UI frameworks.

It often returns Invalid Date, so your comparison (selectedDate < ninetyDaysAgo) silently fails.

 

Replace: 

var selectedDate = new Date(newValue);

 

By: 

var selectedDate = new Date(newValue + 'T00:00:00');

 

 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

GlideFather
Tera Patron

Hi @chandan2212,

have you checked what date format is given and what is required in the code?

When you submit the form, what format do you see in the variable afterwards?

Is it "15.10.2025", "2025-10-15", or any other....?

 

You might need to use getByFormat('DD-MM-YYYY'); //or to select what date format is used

 

Also, if you will have it for onChange it will only display info message but it will let the user submit, so you can keep this to inform them but to abort submission you will need to achieve it by onSubmit CCS...

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


Kieran Anson
Kilo Patron

I'd recommend switching to the no-code approach as documented by @Mark Roethof in the following article:

No Code date validations through (Catalog) UI Policies - ServiceNow Community