Validate end date is after start date when completing end date first

heathers_
Kilo Sage

We have an onChange client script to validate the end date is not before the start date. However, that only works if you fill in the start date first. I also need a validation for the same scenario (end date after start date) if the end date is filled in before the start date.

Here is my current script.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	
	if (isLoading || newValue == '') {
		
		return;
		
	}
	
	
	var end = g_form.getValue('due_date');
	
	var start = g_form.getValue('u_planned_start_date');
	
	// skip if start or end is blank
	
	if (start == "" || end == "") {
		
		return;
		
	}
	
	
	// get user's date time display format
	
	var format =  g_user_date_time_format;
	
	var isEndBeforeStart = compareDates(start, format, end, format);
	
	
	if (isEndBeforeStart) {
		g_form.setValue('due_date', '');
		
		alert("Planned end date must be after Planned start date.");
		
		
	}
}
1 ACCEPTED SOLUTION

Thank you! That did help a bit. I noticed that after both date fields had been filled in and the start date was changed to a date later than the end date, the script did not catch that. I created a second onChange script on the start date.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	
	if (isLoading || newValue == '') {
		
		return;
	}
	
	var end = g_form.getValue('due_date');
	var start = g_form.getValue('u_planned_start_date');
	var format =  g_user_date_time_format;
	var isEndBeforeStart = compareDates(start, format, end, format);
	
	
	// skip if start and end is blank
	
	if (start == "" && end == "") {
		
		return;
	}
	
	// skip if start is not blank and end is blank
	
	if (start != "" && end == ""){
		
		return;
	}
	
	if (isEndBeforeStart) {
		g_form.setValue('u_planned_start_date', '');
		
		alert("Planned start date must be before Planned end date.");
		
		
	}
}

 

 

View solution in original post

3 REPLIES 3

harshinielath
Tera Expert

Hi

Have the onChange script to run when the field 'End Date' is changed instead of start date.

And in script, have something like this:

 

var start = g_form.getValue('u_planned_start_date'); 

if (start == "")

alert('Please enter start Date first);

else{

//code

}

Thank you! That did help a bit. I noticed that after both date fields had been filled in and the start date was changed to a date later than the end date, the script did not catch that. I created a second onChange script on the start date.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	
	if (isLoading || newValue == '') {
		
		return;
	}
	
	var end = g_form.getValue('due_date');
	var start = g_form.getValue('u_planned_start_date');
	var format =  g_user_date_time_format;
	var isEndBeforeStart = compareDates(start, format, end, format);
	
	
	// skip if start and end is blank
	
	if (start == "" && end == "") {
		
		return;
	}
	
	// skip if start is not blank and end is blank
	
	if (start != "" && end == ""){
		
		return;
	}
	
	if (isEndBeforeStart) {
		g_form.setValue('u_planned_start_date', '');
		
		alert("Planned start date must be before Planned end date.");
		
		
	}
}

 

 

Instead of another client script, You can just clear the values(start date and end date) after you display the alert message of start date not correct like this:

if (isEndBeforeStart) {
alert("Planned start date must be before Planned end date.");
g_form.setValue('u_planned_start_date', '');
g_form.setValue('u_planned_end_date', '');

}