Disable Past date in Date variable on Record Producer form.

Namita_Snow
Tera Expert

Hi,

 

I have a requirement to add Date field that should only allow user to select future dates, and we used client-side validation to fulfill the requirement. 

 

I would like to know, if there is there any way to disable past date from the Date field, instead of writing client-side validation script.

 

Thanks in Advance!

7 REPLIES 7

Hi @Namita_Snow,

 Yes. it is client side validations with out using scripts/ code.

 

I agree with @OlaN , there is no way to disable the past dates.

 

If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow

Hi @Namita_Snow ,

 

If our responses helped you could you please mark them as solution accepted as well, so that others can benefit from it.

 

Thanks,

Danish

 

svirkar420
Tera Expert

Hey there Namita!
Although it can be done with UI policy the record can still be submitted. To restrict the record from submitting and adding the error message you can use a "onSubmit catalog client script" as follows!

//script 

function onSubmit() {
    var dateStr = g_form.getValue('your_date_variable_name');
    if (!dateStr) return true; 
    var selectedDate = new Date(dateStr);
    var today = new Date(); 
    // Normalize time to midnight for accurate comparison
    selectedDate.setHours(0, 0, 0, 0);
    today.setHours(0, 0, 0, 0); 
    if (selectedDate < today) {
        g_form.showFieldMsg('your_date_variable_name', 'Past dates are not allowed. Please select a future date.', 'error');
        return false; // Prevent form submission    } 
    return true; // Allow submission
}
Here are some reference screenshots!
 
Please mark my solution as accepted if it helped!
Regards,
Saurabh