Date validation ?

KM SN
Tera Expert

In my scoped application i have a field called planned start date where it should not take past date if so it has to throw error msg and should clear the value but unfortunately its happening for past date, today , and for future date too.

 

what went wrong in my scripts?date on change.PNGdate-si.PNG

8 REPLIES 8

KM SN
Tera Expert

@Ankur Bawiskarany help pls?

Jim Coyne
Kilo Patron

You can do it right in a Client Script:

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

    //get the selected date/time, local time
    var selectedDateLocal = new Date(getDateFromFormat(newValue, g_user_date_time_format));

    //get the current date/time, local time
    var nowLocal = new Date();

    //is the selected date/time in the future?
    if (selectedDateLocal.valueOf() <= nowLocal.valueOf()) {
        g_form.setValue("planned_start_date", "");
        g_form.showFieldMsg("planned_start_date", "Please select a future date/time", "warning");
    }
}

Its showing fieldmsg by clearing value but its happening for every date i select i mean its happening even for todays date and future date even.

 

Note: my field type is Just date

 

 

For a Date field:

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

    //get the selected date, first thing in the morning, local time
    var selectedDateLocal = new Date(getDateFromFormat(newValue, g_user_date_format));
    selectedDateLocal.setHours(0, 0, 0, 0); //set it to first thing in the morning

    //get today's date, first thing in the morning, local time
    var todayLocal = new Date();
    todayLocal.setHours(0, 0, 0, 0); //set to first thing in the morning

    //is the selected date in the past?
    if (selectedDateLocal.valueOf() < todayLocal.valueOf()) {
        g_form.setValue("planned_start_date", "");
        g_form.showFieldMsg("planned_start_date", "Dates in the past are not valid.", "warning");
    }
}