PDF Javascript Issue With Date Selector

Community Beginner ,
Aug 16, 2020

Copy link to clipboard

Copied

I have a PDF with some javascript that's used to calculate the time remaining in a mortgage contract, in years in months, as of the current date. The formula is:

 

Current Date - (Date Loan Closed + # years in the contract).

 

I have the formula calulating correctly, but for some reason, when the dates are entered then changed, whether entered manually or via the drop down, the "Remaining" portion spits out a single, random number. I have the date format set to mm/dd/yyyy.

 

For example, if a user entered "7/13/2005," I have a pop-up saying that the format needs to be corrected (since in this example, there needs to be a zero in front of 7). Once this change occurs, the "Remaining" calculation becomes incorrect. I'm not 100% that this glitch is caused from the date fields, but I can't seem to replicate it any other way.

 

I've attached a video of the error occuring so it might make more sense, as well as the pdf.

TOPICS
PDF forms

Views

120

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

PDF Javascript Issue With Date Selector

Community Beginner ,
Aug 16, 2020

Copy link to clipboard

Copied

I have a PDF with some javascript that's used to calculate the time remaining in a mortgage contract, in years in months, as of the current date. The formula is:

 

Current Date - (Date Loan Closed + # years in the contract).

 

I have the formula calulating correctly, but for some reason, when the dates are entered then changed, whether entered manually or via the drop down, the "Remaining" portion spits out a single, random number. I have the date format set to mm/dd/yyyy.

 

For example, if a user entered "7/13/2005," I have a pop-up saying that the format needs to be corrected (since in this example, there needs to be a zero in front of 7). Once this change occurs, the "Remaining" calculation becomes incorrect. I'm not 100% that this glitch is caused from the date fields, but I can't seem to replicate it any other way.

 

I've attached a video of the error occuring so it might make more sense, as well as the pdf.

TOPICS
PDF forms

Views

121

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
Aug 16, 2020 0
Adobe Community Professional ,
Aug 16, 2020

Copy link to clipboard

Copied

The first observation is that this PDF was created in Adobe Illustrator and exported to PDF.

 

It has a few design glitches and is acting very sluggish.  

 

The second observation is the scripts used to calculate dates are incorrect.  If you're using util.scand to process a date  object, once you're done applying your date script calculations,  you need to convert it back to a date string  object with util.printd or it won't work correctly.

 

You're also missing the appropriate arithmetic between  year, month, days, hours, minutes and miliseconds for accurate results.

 

 

Here's an example:

 

 

var LoanClosed = this.getField("DateLoanClosed").value;
var remaining = this.getField("RemainingYrs").value;
var d = util.scand("mm/dd/yyyy", LoanClosed);  
    d.setDate(d.getDate() - remaining);  
    event.value = util.printd("mm/dd/yyyy", d);  

 

 

 

And in order to calculate the correct difference in days between two dates you need  a different script.

 

You won't need a validattion script if you apply the correct arithmetic and use a date picker for the users, don't let them enter a date manually , unless you apply an arbitrary mask if there's a need for entering dates manually on each field.

 

The script below calculates difference in days between two given dates:

 

 

 

var strStart = this.getField("StatementDate").value;
var strEnd = this.getField("DateLoanClosed").value;

if ( ( strStart != "") && ( strEnd =="") )event.value = "day(s):"

 else 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 = ("day(s):");

  if (diff < "0")  event.value = "";
  else event.value = days;
  
}

else event.value = "";

 

 

It would be easier if you just reflect the difference in total month remaining , not year +months which is why you're getting the errors.

 

 

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...
Aug 16, 2020 0
Community Beginner ,
Aug 16, 2020

Copy link to clipboard

Copied

Thanks for the reply. Here's a couple of my questions:

 

- For the design aspect, I did the first half of the first page in Illustrator and converted it to PDF. The second half of the first page and the entire second page were all created in Acrobat. Is this what's causing the sluggishness?

 

- "The second observation is the scripts used to calculate dates are incorrect.  If you're using util.scand to process a date  object, once you're done applying your date script calculations,  you need to convert it back to a date string  object with util.printd or it won't work correctly."

How do I convert is back? Do I just replace "util.scand" with "util.printd" in "event.value ="?

 

- How do I only allow dates to be selected with the date picker? I don't want any manual additions.

 

- The "Remaining" section is more for visual presentation to loan officers so they don't have to bust out a calculator to divide the number of months remaining by 12. With that being said, if I wanted to keep this field, would I just replace the formula to take the total months remaining divided by 12 to get the number of years, then allocate the fraction remaining for the number of months?

 

I really appreciate you taking time to answer my questions! I'm just starting to learn Javascript, so your advice is awesome!

 

 

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...
Aug 16, 2020 1
Community Beginner ,
Aug 19, 2020

Copy link to clipboard

Copied

@ls_rbls, did you see my previous reply with the questions I had (in the red font)? I'm still having trouble.

 

Any help is much appreciated!

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...
Aug 19, 2020 0
Most Valuable Participant ,
Aug 19, 2020

Copy link to clipboard

Copied

Jumping in here:

 

- How do I convert is back? Do I just replace "util.scand" with "util.printd" in "event.value ="?

> Yes, pretty much.

 

- How do I only allow dates to be selected with the date picker? I don't want any manual additions.

> You can't do that.

 

- The "Remaining" section is more for visual presentation to loan officers so they don't have to bust out a calculator to divide the number of months remaining by 12. With that being said, if I wanted to keep this field, would I just replace the formula to take the total months remaining divided by 12 to get the number of years, then allocate the fraction remaining for the number of months?

> Correct.

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...
Aug 19, 2020 0
Adobe Community Professional ,
Aug 21, 2020

Copy link to clipboard

Copied

Hey jons,

 

I am so sorry to not follow up with you quicker.

 

I am working with some things in your PDF and I will reply back to you with some more ideas. This has taken me longer than expected. 

 

I apologize for taking this long.

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...
Aug 21, 2020 1
Community Beginner ,
Aug 24, 2020

Copy link to clipboard

Copied

No problem! I appreciate 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...
Aug 24, 2020 0
Adobe Community Professional ,
Aug 29, 2020

Copy link to clipboard

Copied

+++++ EDITED REPLY 

 

Hey jons,

 

So I was able to reduce the file size of the original file that you shared from 1,006.58KB to 286.33 KB.

 

As far as the "MortStatementDate" field in the first page you don't need to use a validation script to alert users on how to enter the desired format. Once you've applied the customized date format in this field the validation script is not necessary.

 

The date field will automatically throw this alert and show the user what is the intended date format.   However if you still want to keep that validation script, a good idea would be  to encourage the user(s) to choose to choose a date from the date picker instead of typing.

 

Now that the design part is fixed we can work on the scripting part more comfortably..

 

Another observation is, since this form  is only two pages long but huge in terms of JavaScripts, that it would be convenient to incorporate some document-level scripting.

 

This may aleviate the sluggish behavior of all the current calculated fields that are trying to perform functions every time you fill or make a selection in other blank fields.

 

By the way, you did a good job. This form is very complicated and I can see all the great effort this required from you.  Most of every date fields are working good with the method that you used but we can improve some of the scripts.  

 

 

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...
Aug 29, 2020 0
Adobe Community Professional ,
Aug 30, 2020

Copy link to clipboard

Copied

+++++UPDATE,

 

I think I was able to fix the problem with the dates without modifying your original code too much.

 

Please evaluate the new form and let me know if it is working correctly:

 

 

The problem that you described in your initial inquiry happens with both, manual entry of the date and also using the date picker, in the "MortStatementDate" field. If you continue to work with other fields, the date field(s) will update. To work around this annoyance, You may need to re-arrange the calculation order of the fields.

 

On a side note, you'll also notice that the date variables are now declared with ".valueAsString" instead of ".value".    

 

I also added a custom calculation script on each date field in addition to using the Acrobat built-in custom date formats:

 

 

if (event.value !=="") {
var d = util.scand("mm/dd/yyyy", event.target.value);
    d.setDate(d.getDate());  
    event.value = util.printd("mm/dd/yyyy", d);  
}


 

 

I've noticed in my forms that adding this as a custon calc script  allow the manual entry of a dates like, for example,  "8/27", "AUG27", "aug27", "aug, 27", and  wihtout the need of a validation script, and without throwing any errors.

 

I also noticed that you had a custom value in the "DateLoanClosed"  to 01 January 2020.

 

Everytime the form was reset it would display  01/01/2020  and causing the "210DayRule" field to autocalculate itself when all the form fields were blank.

 

I am not sure why you needed that default value there, but I took the initiavive of deleting that default value.

 

Last, you''l also notice that the "RemainingYrs" field will remain blank until all the fields that its calculated script calls for are populated with values (these are :this.getField("DateLoanClosed").value, this.getField("MortStatementDate").value, this.getField("LoanTerm").value).

 

One more thing, the name of the borrowers and the today's date field values autopopulate in the scond page; I made those fields read only. There's no need to enter the same info twice so the user can just continue to work onpage 2 without stopping the workflow to type in this info all over again.

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...
Aug 30, 2020 0
Adobe Community Professional ,
Aug 31, 2020

Copy link to clipboard

Copied

+++ Quick caveat on this inquiry:

 

  • - How do I only allow dates to be selected with the date picker? I don't want any manual additions.

    > You can't do that.

 

Technically you can't, but you can fool the PDF users who never read or want to follow basic instructions.

 

You can use a mouse exit action script in the "MortStatementDate" field which will likely prevent any further typing in that field , and even make the date picker useless for this purpose:

 

Use this small line of code:

 

var date  =  event.target;
date.value = util.printd("mm/dd/yyyy", new Date());

 

It won't matter what the users try to do,  the moment the mouse pointer enters  and then exits that field, the current date for that day will continue to output the date in the appropriate format that you have chosen to be displayed.

 

I think this is pretty close to not alllowing users to have any manual additionas in this particular  date field. You can still use the date picker to change the current date and save the document, but with manual keystroking it won't a llow the change to happen incorrectly.

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...
Aug 31, 2020 0
Most Valuable Participant ,
Aug 31, 2020

Copy link to clipboard

Copied

That would overwrite any value the selected in the date picker, though... You might as well just have a calculated value, then.

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...
Aug 31, 2020 0
Adobe Community Professional ,
Aug 31, 2020

Copy link to clipboard

Copied

Is just for the current date though (today's date).

 

I wasn't looking into the fact  that a loan application may need to be pre-dated after it is filled out.

 

Although it is convenient,  in the case of governement forms, for example, pre-dating a document with the intent to demonstrate that an event took place in a certain day (when it didn't) may be considered unethical.

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...
Aug 31, 2020 0
Most Valuable Participant ,
Aug 31, 2020

Copy link to clipboard

Copied

If the field should only show the current date there's no reason to use a date picker at all. It can have a calculated value, or can be populated through a button field (if you don't want it updating each time the file is opened).

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...
Aug 31, 2020 0
ls_rbls LATEST
Adobe Community Professional ,
Aug 31, 2020

Copy link to clipboard

Copied

Oooh OK.  I didn't even considered that part everytime the document opens... cool!

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...
Aug 31, 2020 0