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

set limit to date field

Explorer ,
Jun 28, 2020 Jun 28, 2020

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
1.6K
Translate
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) {
Translate
Community Expert ,
Jun 29, 2020 Jun 29, 2020

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

Translate
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
Explorer ,
Jun 30, 2020 Jun 30, 2020

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

Translate
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

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

Translate
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
Explorer ,
Jul 01, 2020 Jul 01, 2020

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

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;

Translate
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
Explorer ,
Jul 02, 2020 Jul 02, 2020

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.

Translate
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

In the second script remove .value from:

if (days.value > 730) {
Translate
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
Explorer ,
Jul 03, 2020 Jul 03, 2020
LATEST

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

Translate
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

This free tool makes things easier: http://practicalpdf.com/the-practicalpdf-date-library-for-adobe-acrobat/


Acrobate du PDF, InDesigner et Photoshopographe
Translate
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