date variable

abhisek
Tera Contributor

Hi All,

I have a date variable 'xyz' which value is getting auto populated based on other 2 variable's values but still not read only. When the value of the date variable 'xyz' is not empty then I need to restrict the user to select any date which is greater than the current value of that date variable xyz that means user can select any date till the date which is already getting auto populating but not greater than that. 

I want to do it using on change client script.

 

Can you please help me out.

Thanks in advance.

 

Regards,

Abhisek Chattaraj.

 

 

 

15 REPLIES 15

Hi @MukkamalaM 

Thanks for your reply.

I am not allowed to create another hidden variable.

 

Thanks&Regards,

Abhisek Chattaraj.

@abhisek  

Please use the following script and make adjustments as necessary!
 
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   if(oldValue && newValue){
    if(newValue>oldValue){
        g_form.clearValue('date1'); //add variable name here in place of date1
        g_form.addErrorMessage('Date should be less than auto-populated');
    }
   
   }

   //Type appropriate comment here, and begin script below
   
}

Juhi Poddar
Kilo Patron

Hello @abhisek  

Try this onChange client script to meet your requirement:

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

    if (oldValue && newValue) {
        // Convert the old and new values to Date objects
    var oldDate = new Date(oldValue);
    var newDate = new Date(newValue);
        // Compare the new date with the old date
        if (newDate > oldDate) {
            // Show field error message if the new date is greater than the old date
            g_form.showErrorBox('date','date cannot be greater than autopopulated value');
            //g_form.setValue('date', oldValue); // Optionally, reset the field to the old value (auto-populated)
        } else {
            // Hide the error message if the new date is valid
            g_form.hideErrorBox('date');
        }
    }

}

Result:

JuhiPoddar_0-1735915608496.png

JuhiPoddar_1-1735915636834.png

Here the auto-populated value is '2025-01-05'

 Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Hi @Juhi Poddar 

Thanks for your reply.

It is not working.

 

Regards,

Abhisek Chattaraj.

Hello @abhisek  

Can you please share your script?

Additionally update the variable name for showErrorBox and HideErrorBox. In the script I have mentioned it as 'date'.

 

Thank You

Juhi Poddar