• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

set limit to date field

New Here ,
Jun 28, 2020 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?

TOPICS
Acrobat SDK and JavaScript

Views

757

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
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 02, 2020 Jul 02, 2020

In the second script remove .value from:

if (days.value > 730) {

Votes

Translate

Translate
Community Expert ,
Jun 29, 2020 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

Votes

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
community guidelines
New Here ,
Jun 30, 2020 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?

Votes

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
community guidelines
Community Expert ,
Jul 01, 2020 Jul 01, 2020

Copy link to clipboard

Copied

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

Votes

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
community guidelines
New Here ,
Jul 01, 2020 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.");
}
}

Votes

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
community guidelines
Community Expert ,
Jul 02, 2020 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;

Votes

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
community guidelines
New Here ,
Jul 02, 2020 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.

Votes

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
community guidelines
Community Expert ,
Jul 02, 2020 Jul 02, 2020

Copy link to clipboard

Copied

In the second script remove .value from:

if (days.value > 730) {

Votes

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
community guidelines
New Here ,
Jul 03, 2020 Jul 03, 2020

Copy link to clipboard

Copied

LATEST

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

Votes

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
community guidelines
Community Expert ,
Jul 02, 2020 Jul 02, 2020

Copy link to clipboard

Copied

Votes

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
community guidelines