Allow past date in date/time field

Martines
Tera Contributor

Hi community, 

 

I'm struggling with creating the date/time field where only the past dates are allowed.  I found this code. 

Martines_0-1770898724144.png

But it doesn't work. The accepted behavior is that user is not able to choose from the date/time component the future dates. (This would be happy case) Or at least when user choose date in future he will get the message that future dates are not allowed. 

 

Do you have any suggestions? Thank you in advance. 

9 REPLIES 9

Mark Manders
Mega Patron

Same ui policy, but update the text in the script. The condition should be 'start date relative after 1 minute ago' or something similar.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

yashkamde
Tera Guru

Hello @Martines ,

 

You can use the onChange client script for that :

Screenshot 2026-02-12 180100.png

 

Script :

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   //Type appropriate comment here, and begin script below

    var currentDate = new Date();
    var selectedDate = new Date(newValue);

    if (selectedDate.valueOf() > currentDate.valueOf()) {
        g_form.addErrorMessage('Future dates are not allowed. Please select a past date.');
        g_form.clearValue('your_date_field');
    }
}

 

If my response helped mark as helpful and accept the solution.

thank you for your answer. And if I have more fields where I need to apply this? I don't want to create like 6 ciient scripts for each field I would like to have like on function for that. 

Client scripts work on the fields themselves, so you will need one for every field. But it makes no sense. You can do this with ui policies.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark