Need to validate the end date is not before the start date onSubmit

Julie Catano
Kilo Guru

When a customer is requesting a report for a period of time from start date to end date.  I need to validate onSubmit that the end date is not before the start date and it needs to not allow it to be submitted until corrected.

 

My variable field names - 'time_period_of_interest_start_date' and 'time_period_of_interest_end_date'

 

6 REPLIES 6

Tudor
Tera Guru

Hi Julie,

My suggestion would be to have 2 onChange client scripts that compare the 2 values.

1. field - time_period_of_interest_start_date

If (newValue>time_period_of_interest_end_date){

g_form.clearValue('time_period_of_interest_start_date');

}

2 field - time_period_of_interest_end_date

If (time_period_of_interest_start_date>newValue){

g_form.clearValue('time_period_of_interest_end_date');

}

PS: don't forget to make the fields mandatory from their definition or UI Policy.

Hope this helps!

 Tudor

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Julie,

If both fields are of type "Date", following script will validate if the end date is after start date.

function onSubmit() {
    var startDate = getDateFromFormat(g_form.getValue('time_period_of_interest_start_date'), g_user_date_format);
    var endDate = getDateFromFormat(g_form.getValue('time_period_of_interest_end_date'), g_user_date_format);

	if (startDate > endDate) {
		g_form.clearMessages();
		g_form.addErrorMessage('End date should be after start date');
		return false;
	}
}

Execution result:

find_real_file.png

BTW, if the type if "Date/Time", use the following script.

function onSubmit() {
    var startDate = getDateFromFormat(g_form.getValue('time_period_of_interest_start_date'), g_user_date_time_format);
    var endDate = getDateFromFormat(g_form.getValue('time_period_of_interest_end_date'), g_user_date_time_format);

	if (startDate > endDate) {
		g_form.clearMessages();
		g_form.addErrorMessage('End date should be after start date');
		return false;
	}
}

Julie Catano
Kilo Guru

I created an OnSubmit and I didn't get an error, it submitted without an error.