Restrict Date Selection to Between 3 and 6 Weeks After Submission Date

vivek11
Tera Contributor

 

  • Hi team,

  •  Can Someone help me please

    Users can only select a date that is in the future and 3 to 6 weeks after the submission date.

  • Past dates (before today) and the date of submission itself are not allowed.

 

 

1 REPLY 1

Community Alums
Not applicable

Not sure what you meant by " date of submission".
Lets say the date field name is "due_date"
If it is the current date, then just use the below script as it is
If its another date field, then just replace the "now" variable with the "date of submission"

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return; // Skip if the form is loading or the field is empty
    }

    // Get the selected date from the field
    var selectedDate = new GlideDateTime(newValue);

    // Get the current date
    var now = new GlideDateTime();
    now.setValue(gs.nowDateTime()); 

    // Calculate the minimum date (3 weeks from now)
    var minDate = new GlideDateTime(now);
    minDate.addWeeks(3);

    // Calculate the maximum date (6 weeks from now)
    var maxDate = new GlideDateTime(now);
    maxDate.addWeeks(6);

    // Compare the selected date with the allowed range
    if (selectedDate.before(minDate) || selectedDate.after(maxDate)) {
        // Show an alert to the user
        g_form.addErrorMessage('Please select a date between 3 and 6 weeks from today.');
        // Clear the invalid value
        g_form.clearValue('due_date');
    }
}