How to validate start dates and end dates
Copy link to clipboard
Copied
I have a form related to contracts. There are two fields for the contract terms: start date and end date.
My wish for the validation of these two fields:
1) That users are alerted to enter a start date before they enter an end date
2) That users are alerted to correct the end date if it comes before the start date
Here was my attempt:
event.rc = true;
if (event.value) {
var startDateString = this.getField("StartDate_af_date").value;
if (startDateString=="") {
app.alert("You must first fill-in the contract start date.");
event.rc = false;
} else {
var startDate = this.getField("StartDate_af_date").value;
var endDate = event.value;
if (endDate>startDate) {
app.alert("End date of the contract must be AFTER start date. Please correct.");
event.rc = false;
}
}
}
No matter what end date is entered, the alert pops up.
I'm a self-taught scripter and cobbled this together from another forum answer. I know I am missing something critical here but my research hasn't helped me.
Thank you!
Copy link to clipboard
Copied
You can't compare date strings like that. It's more complicated than that.
You need to convert them from strings to Date objects (using the scand method of the util object) and then you'll be able to compare them using the getTime method.
See these tutorials:
https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript
https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-2
https://acrobatusers.com/tutorials/working-date-and-time-acrobat-javascript-part-3-3

