Date field variable restriction for current, past and able to select max 365 days from tomorrow

s_dinesh01
Tera Expert

Hello,

 

i have a requirement for one of the catalog variable of date type field, where i should restrict the user on selecting present, past date and user should be able to select only max 365 days from date of selection that's from tomorrow, anyone help me out how this can be achieved.

 

Thankyou

2 REPLIES 2

Sid_Takali
Kilo Patron
Kilo Patron

Hi @s_dinesh01 Write onChange Catalog Client Script on Date Variable 

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

    var selectedDate = new Date(newValue);
    var currentDate = new Date();
    
    var minDate = new Date();
    minDate.setDate(minDate.getDate() + 1); 
    
    var maxDate = new Date();
    maxDate.setDate(maxDate.getDate() + 365); 

    if (selectedDate < minDate) {
        g_form.addErrorMessage('The selected date cannot be in the past. Please choose a date from tomorrow onwards.');
        g_form.setValue('your_date_variable_name', ''); 
    } else if (selectedDate > maxDate) {
        g_form.addErrorMessage('The selected date cannot be more than 365 days from today.');
        g_form.setValue('your_date_variable_name', ''); 
    }
}

 

Regards,

Sid

Voona Rohila
Kilo Patron
Kilo Patron

Hi @s_dinesh01 

You can simply write UI Policy with no code. Refer below link for explanation and examples

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP