set limit to date field

New Here ,
Jun 28, 2020

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?

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

In the second script remove .value from:

if (days.value > 730) {
TOPICS
Acrobat SDK and JavaScript

Views

131

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

set limit to date field

New Here ,
Jun 28, 2020

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?

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

In the second script remove .value from:

if (days.value > 730) {
TOPICS
Acrobat SDK and JavaScript

Views

132

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 28, 2020 0
Most Valuable Participant ,
Jun 29, 2020

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 29, 2020 0
New Here ,
Jun 30, 2020

Copy link to clipboard

Copied

Those tutorials are 14 years old and don't seem to be very helpful. Any other suggestions?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 30, 2020 0
Most Valuable Participant ,
Jul 01, 2020

Copy link to clipboard

Copied

They are still relevant, though. Do you have specific issues with them?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 01, 2020 0
New Here ,
Jul 01, 2020

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.");
}
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 01, 2020 0
Most Valuable Participant ,
Jul 02, 2020

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;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 02, 2020 0
New Here ,
Jul 02, 2020

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 02, 2020 0
Adobe Community Professional ,
Jul 02, 2020

Copy link to clipboard

Copied

In the second script remove .value from:

if (days.value > 730) {

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 02, 2020 0
New Here ,
Jul 03, 2020

Copy link to clipboard

Copied

Bingo! Success! Date fields are a pain in the @#$%! Thanks for your help.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 03, 2020 0
Adobe Community Professional ,
Jul 02, 2020

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 02, 2020 0