Copy link to clipboard
Copied
I have a form with two date fields, "certDate" and "expireDate" (both set to mm/dd/yyyy format). Once a date is entered into the first field, "certDate", the date entered into the second field, "expireDate", can't be more than two years later than the first date. It can be less than two years later than the first date, but can't be earlier than the first date. How should "expireDate" be scripted?
In the second script remove .value from:
if (days.value > 730) {
Copy link to clipboard
Copied
You can write a custom validation script to do it. You need to convert the string values of the fields to Date objects and then you'll be able to manipulate those objects and check they don't pass a certain threshold. If they do, you can reject the user's value by setting event.rc as false in your code.
See:
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
Copy link to clipboard
Copied
Those tutorials are 14 years old and don't seem to be very helpful. Any other suggestions?
Copy link to clipboard
Copied
They are still relevant, though. Do you have specific issues with them?
Copy link to clipboard
Copied
Well, for starters, the two scripts discussed in part 1 don't work, at least not on my computer. The first one returns an error message of "theNewDate is not defined", and the second returns an error message of "strMessage is not defined". In part 2, Mr. Parker states that the "Order Date" field of his attached form has been set to "read only" when in fact it has not, and states that the 3rd line of his script should be "if(strStart.length || strEnd.length)" while his attached form says "if(strStart.length && strEnd.length)". The script in part 2 seems to work in his attached form, but when copied directly from part 2 into the console in Acrobat on my computer returns an error of "days is not defined". Clearly Mr. Parker's tutorials are riddled with typos.
This is the (successful) script in my first date field:
var date = util.scand("mm/dd/yyyy", this.getField("certDate").value);
var expire = this.getField("expireDate");
date.setDate(date.getDate()+730)
if (this.getField("certDate").value!="")
{
expire.value = util.printd("mm/dd/yyyy",date)
}
else
{expire.value = ""}
This is the (unsuccessful) validation script in my second date field:
var strStart = this.getField("certDate").value;
var strEnd = this.getField("expireDate").value;
if(strStart.length || strEnd.length) {
var dateStart = util.scand("mm/dd/yyyy",strStart);
var dateEnd = util.scand("mm/dd/yyyy",strEnd);
var diff = dateEnd.getTime() - dateStart.getTime();
var oneDay = 24 * 60 * 60 * 1000;
var days = Math.floor(diff/oneDay);
if (days.value > 730) {
app.alert("The expiration date cannot be more than two years later than the certificate signature date.");
}
}
Copy link to clipboard
Copied
As far as I know it should all still work. Please provide more details as to how it's not working for you.
In the second code change this line:
var strEnd = this.getField("expireDate").value;
To:
var strEnd = event.value;
Copy link to clipboard
Copied
Absolutely nothing happens. The validation code might as well not exist. The user can enter anything into the second field. Your suggested code change has no effect whatsoever.
Copy link to clipboard
Copied
In the second script remove .value from:
if (days.value > 730) {
Copy link to clipboard
Copied
Bingo! Success! Date fields are a pain in the @#$%! Thanks for your help.
Copy link to clipboard
Copied
This free tool makes things easier: http://practicalpdf.com/the-practicalpdf-date-library-for-adobe-acrobat/