I AM unable to set the future date is only one day in catalog item

Prem96
Tera Contributor

I have one date field (Accept date) in that we will need to select only one future date (like, today is 20 when im doing this i can able to select only 20 or 21) if i select after this date, 22,23, like this alert will trigger for that i write a  onchange  client script.

could you please help me on this.

 

var endSNDate = g_form.getValue("u_accept_date");
    var todayJSDate = new Date();
    var endJSDate = new Date(endSNDate);
    var timeDiff = endJSDate.getTime() - todayJSDate.getTime();
    var dayDiff = timeDiff;
    var todaySNDate = new Date().getFullYear() + '-' + ("0" + (new Date().getMonth() + 1)).slice(-2) + '-' + ("0" + new Date().getDate()).slice(-2);

    if (todaySNDate > endSNDate) {
        alert("Please select date range feture date only");
        g_form.setValue('u_accept_date', '');
    }
    if ((endSNDate > todaySNDate) && (dayDiff > 10)) {
        alert("Please select date range feture date only");
        g_form.setValue('u_accept_date', '');

    }
1 REPLY 1

Deepak Shaerma
Kilo Sage

Hi @Prem96 

Name: Give your client script a meaningful name, e.g., “Validate Accept Date”.
Table: Choose the table where your “Accept date” field is located.
UI Type: Make sure it’s set to “All” or “Desktop” depending on where you want the script to run.
Type: Choose “onChange”.
Field name: Select the field you want to apply the script to, which in your case is the “Accept date” field.

var selectedDate = new Date(newValue);
    var today = new Date();
    var tomorrow = new Date();
    tomorrow.setDate(today.getDate() + 1);

    // Reset hours, minutes, seconds for accurate comparison
    today.setHours(0,0,0, 0);
    tomorrow.setHours(0, 0, 0, 0);
    selectedDate.setHours(0, 0, 0, 0);

    // Check if selected date is beyond tomorrow
    if (selectedDate > tomorrow) {
        alert("Please select today’s or tomorrow’s date.");
        
        // Optionally, clear the field or set it to a default value
        g_form.setValue('your_field_name', ''); // Replace ‘your_field_name’ with the actual field name
        // Or set to today’s date
        //g_form.setValue('your_field_name', g_form.getValue('sys_date')); // Assuming ‘sys_date’ holds today’s date in the correct format
    }
}

Remember to replace 'your_field_name' with the actual name of your “Accept date” field, ensuring the field name matches exactly.
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards 
Deepak Sharma