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

Document JavaScript and Validation

New Here ,
Oct 19, 2021 Oct 19, 2021

My Adobe Acrobat was going slow due to the calculations I had. So I moved the calculations into a Document JavaScript. Below is one example of 20 calculation functions. The I go to the Total01 text Field and select run custom Validation Script. In there I have Total01(); However it does not work what am I missing?

function Total01()
{
    // Get the field values, as numbers
    var v1 = this.getField("Cost01").valueAsString;
    var v2 = this.getField("Trips01").valueAsString;

    // Multiply the values
    var result = v1 * v2;

    // Set the value of this field, rounding to two decimal places
    Total01=result;
};

 

TOPICS
JavaScript
1.7K
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
LEGEND ,
Oct 19, 2021 Oct 19, 2021

1. It's not safe to use "this" in a function, because the meaning of "this" changes according to where it is run. Pass it as a parameter.

 

2. I think you are expecting

Total01=result;

to do something other than set a variable called Total01. Like set a field? Compare with what you do in the former calculation, before you made a function.

 

3. Why use valueAsString when you want a number?

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
New Here ,
Oct 19, 2021 Oct 19, 2021

I changed the valueAsString to value as I realized my mistake on that one.

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
LEGEND ,
Oct 19, 2021 Oct 19, 2021

PS: moving calculations to functions is recommended, because it is easier to maintain. It won't make anything faster if the number of calculations is the same! You have to reduce the number of calculations.

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
New Here ,
Oct 19, 2021 Oct 19, 2021

Thanks for the information. I am not a JavaScript person, so I am unsure how to pass it as a parameter. I pieced milled this together by looking around online and testing.

My problem was with all the calculations. The form was sluggish when filling it out. I read that moving it to a Document JavaScript would speed it up each time you change a field. It refreshes them all no matter what.

I only want to calculate if there is a change in the fields; otherwise, leave it at $0.00

Can you point me in a direction on how to do this? I have been Googling for 5 hours now to no avail.

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 ,
Oct 19, 2021 Oct 19, 2021

Scripts at validation makes only sense on fields where users enter data.

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
New Here ,
Oct 19, 2021 Oct 19, 2021

I only want it to calculate when a user changes the field.

Attached is a screenshot of where 90% of the calculations are occurring.

For each Total and SubTotal field, I had a Calculate field with Value is the product (x) with fields from their respective row as the input.

This worked fine. The problem when you select the next field it takes upwards of 10 seconds for the form to become responsive.

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 ,
Oct 19, 2021 Oct 19, 2021

What field does the user change?

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
New Here ,
Oct 19, 2021 Oct 19, 2021

The user can change any of the miles, cost, days, trips, hours. Then the Total field is read only calculated

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 ,
Oct 19, 2021 Oct 19, 2021

When you want use validation scripts you must use scripts at all this fields.

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 ,
Oct 19, 2021 Oct 19, 2021

Change the last line to:

event.value = result;

 

Also, just moving the calculation scripts from the fields themselves to a doc-level function will not change the speed in which they run.

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
New Here ,
Oct 19, 2021 Oct 19, 2021

Just to make sure I wasn't going down a rabbit hole.

 

  1. I cleared all calculations and JavaScript from the form.
  2. Named all fields correctly and uniformed
  3. Reentered the calculations

 

If you start off at Airfare/Train (Tickets/Exchanges/Agent Fees) enter in a cost amount then press tab. You will see it takes a second to calculate. When more data is entered in the fields the responsivness gradually slows. Then adding in digital signatures adds 5 seconds.

 

I thought that if I moved the calculations into a Document JavaScript then put the function into the valdiation it would only run when there is a change. I might be misunderstanding how validation works. I basically only want to calculate with a change.event.

 

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 ,
Oct 19, 2021 Oct 19, 2021

Works fine for me.

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
New Here ,
Oct 19, 2021 Oct 19, 2021

Ok then. For me it goes slow. I apprecaite you testing it.

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
New Here ,
Oct 20, 2021 Oct 20, 2021

Sorry. It is still moving slow I had 4 others test it and the report that when the press tab the see an hour glass with appears to be a keyobard behind it. It takes 4 seconds for it to go away.

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 ,
Oct 20, 2021 Oct 20, 2021

Yes, it is slow.

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
New Here ,
Oct 20, 2021 Oct 20, 2021

Thank You I am currently looking at this thread as an option

 

https://answers.acrobatusers.com/stop-calculation-user-data-entered-textfield-q293858.aspx

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 ,
Oct 20, 2021 Oct 20, 2021

The calculation is fast with the unsupported version XI.

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
New Here ,
Sep 01, 2022 Sep 01, 2022

After year and revisting this I was able to solve the problem of the constant recalcuation.

 

For the Total filed I went to custom calculation and added the following

// Get value of fields as a number
var n1 = +getField("Cost01").value;
var n2 = +getField("Days01").value;
var n3 = +getField("Trips01").value;
// Set this field's value
event.value = (n1 * n2) * n3;

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 ,
Sep 02, 2022 Sep 02, 2022

"moving calculations to functions is recommended, because it is easier to maintain. It won't make anything faster if the number of calculations is the same! You have to reduce the number of calculations."

Since each user event triggers all calculations in all calculated fields one after the other, the killer trick is to use a single trigger in only one field that does all the calculations, so that it really goes faster.

 

(Thanks to Thom Parker for this useful tip!)


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
New Here ,
Sep 13, 2022 Sep 13, 2022
LATEST

Yeah I thought I had this working but it slowed back down.

 

I am now trying the below to test the speed. Not sure how to do a single trigger like you mentioned

var v1 = this.getField("Cost01").valueAsString;
var v2 = this.getField("Trips01").valueAsString;


if (v1=="" && v2=="") event.value = "";
else {
	var Cost01 = Number(v1);
	var Trips01 = Number(v2);
	event.value = Cost01 * Trips01;
}

 

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