The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to validate start date and end date using client script ?

Vishal Kumar Sr
Tera Contributor

Hi,

I have a scenario to validate the start date and end date like:

I) start date should not be null.==>which is working fine with my code.

II)end date can not be in past. it should be greater than or equal to start date=> my code is working for only current month and give me error once user select end date before from start date for current month. I find it is just checking the date part not checking the month and year part to validate.

III)I tried some Js date methods to get current month, date and year for each date, which is not working in my case. Is there any other supported method to get current date, month and year from date?

I'm posting below the code I'm using. Please help me to modify this to achieve the above points ASAP.

 

Thanks,

Vishal Kumar

 

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

   if (isLoading || newValue === '') {

      return;

   }

 

   //Start Date should not be null..

    if (g_form.getValue('start_date') == "") {

        g_form.clearValue('due_date');

        g_form.showFieldMsg('due_date', "Please select a Start date first", 'error');

    }

   // Due date must be equal to or greater than the start date..

    var startDate1 = g_form.getValue('start_date');

    var dueDate1 = g_form.getValue('due_date');

                var dateArr = startDate1.split(' ');

     var sdp = dateArr[0];

                alert(sdp);  

                var dateArr2=dueDate1.split(' ');

                var ddp=dateArr2[0];

                alert(ddp);

                checkDueDate(startDate1,dueDate1);

                function checkDueDate(startDate1,dueDate1)

                {

                                if(ddp<sdp)

                                {

                                      g_form.clearValue('due_date');

                                    alert('End date Can not be in Past');

                                 g_form.showFieldMsg('due_date', "The due date must be equal to or greater than the start date.       Please select another date.", 'error');

        }

                               

    }

}

 

 

3 REPLIES 3

Shiva Kumar8
Kilo Guru

Hi please refer this link to get the issue fixed

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi,

It would be easier to use UI Policy to compare start date and end date.

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi,

If have to use Client Script.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (g_form.getValue('start_date') == "") {
        g_form.clearValue('due_date');
        g_form.showFieldMsg('due_date', "Please select a Start date first", 'error');
    }
    var startDate = g_form.getValue('start_date');
    var dueDate = g_form.getValue('due_date');
    var startDateNum = getDateFromFormat(startDate, g_user_date_format);  // change to g_user_date_time_format if field type is date/time
    var dueDateNum = getDateFromFormat(dueDate, g_user_date_format); // change to g_user_date_time_format if field type is date/time
    if (dueDateNum < startDateNum) {
        g_form.showFieldMsg('due_date', "The due date must be equal to or greater than the start date.       Please select another date.", 'error');
    }
}

Execution result

Case OK:

find_real_file.png

Case Error:

find_real_file.png