Skip to main content
Participant
May 26, 2020
Answered

two date fields, one can't be before another

  • May 26, 2020
  • 1 reply
  • 2075 views

I need to make sure the End Date falls after the Start Date in my fillable pdf, can someone help with the script?

 

Start Date: MM DD YYY

End Date: MM DD YYY

 

I have attached the pdf, thank you!!

This topic has been closed for replies.
Correct answer try67

You can use this code as the custom calculation script of a (hidden) text field:

 

var startDateMonth = this.getField("part3.month").valueAsString;
var startDateDay = this.getField("part3.day1").valueAsString;
var startDateYear = this.getField("part3.year").valueAsString;
var endDateMonth = this.getField("part3b.month").valueAsString;
var endDateDay = this.getField("part3b.day1").valueAsString;
var endDateYear = this.getField("part3b.year").valueAsString;

if (startDateMonth && startDateDay && startDateYear && endDateMonth && endDateDay && endDateYear) {
	var startDate = util.scand("mmm dd yyyy", startDateMonth + " " + startDateDay + " "+ startDateYear);
	var endDate = util.scand("mmm dd yyyy", endDateMonth + " " + endDateDay + " "+ endDateYear);
	if (startDate && endDate && startDate.getTime()>=endDate.getTime()) {
		app.alert("Error! The end date must be after the start date.");
		this.resetForm(["part3b.month", "part3b.day1", "part3b.year"]);
	}
}

1 reply

try67
Community Expert
Community Expert
May 26, 2020

This is a bit complicated to do with individual fields for month, day and year, as you would first need to validate that all the fields are filled-in, before merging them into a single string, then converting that string into a Date object and compared it with the Date object from the other field.

Can you combine them to a single field, you think?

sereenyAuthor
Participant
May 26, 2020

Unfortunately no, these fields have to be individual, can you help?

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
May 27, 2020

You can use this code as the custom calculation script of a (hidden) text field:

 

var startDateMonth = this.getField("part3.month").valueAsString;
var startDateDay = this.getField("part3.day1").valueAsString;
var startDateYear = this.getField("part3.year").valueAsString;
var endDateMonth = this.getField("part3b.month").valueAsString;
var endDateDay = this.getField("part3b.day1").valueAsString;
var endDateYear = this.getField("part3b.year").valueAsString;

if (startDateMonth && startDateDay && startDateYear && endDateMonth && endDateDay && endDateYear) {
	var startDate = util.scand("mmm dd yyyy", startDateMonth + " " + startDateDay + " "+ startDateYear);
	var endDate = util.scand("mmm dd yyyy", endDateMonth + " " + endDateDay + " "+ endDateYear);
	if (startDate && endDate && startDate.getTime()>=endDate.getTime()) {
		app.alert("Error! The end date must be after the start date.");
		this.resetForm(["part3b.month", "part3b.day1", "part3b.year"]);
	}
}