These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Text field calculations only work sometimes

ForeverSun
Registered: Dec 4 2009
Posts: 8

I am working on a form that needs to calculate the number of packages being ordered. There are 10 packages available. The calculation works correctly when the user orders some of all of the packages, when the user orders just one package, and when the user orders packages in order starting with the first package and without gaps, e.g., orders packages 1, 2, 3 not packages 1,3,5. In the first example, the total comes out correctly as "6". In the second example, the total comes out as "135".

Following is the code:

event.value = (this.getField("# of packages IM").value + this.getField("# of packages IM w 1c").value + this.getField("# of packages IM w 4c").value + this.getField("# of packages IG").value + this.getField("# of packages IG w 1c").value + this.getField("# of packages IG w 4c").value + this.getField("# of packages Pro").value + this.getField("# of packages Pro T").value + this.getField("# of packages BC").value + this.getField("# of packages M").value)

A variation of the same code works perfectly for calculating total cost:
event.value = (this.getField("Total cost of IM").value + this.getField("Total cost of IM w 1c").value + this.getField("Total cost of IM w 4c").value + this.getField("Total cost of IG").value + this.getField("Total cost of IG w 1c").value + this.getField("Total cost of IG w 4c").value + this.getField("Total cost of Pro").value + this.getField("Total cost of Pro T").value + this.getField("Total cost of BC").value + this.getField("Total cost of M").value);
this.getField("Total cost").display = display.hidden;
if (event.value > 0.00)
this.getField("Total cost").display = display.visible

I am not sure why the code works correctly for total cost and not for total packages. Is it because in the case of total packages, the text fields used for the calculation are filled out by the user and in the case of total cost, the text fields used for the calculation are automatically populated based on the users inputs for the number of packages?

This is my first attempt at an Acrobat form and I could really use some help. The rest of the form is working properly.

My Product Information:
Acrobat Pro 9.2, Macintosh
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Are you sure all the fields are set as Number? Or are some of them Text?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

ForeverSun
Registered: Dec 4 2009
Posts: 8
Thank you for the reply. I found one field set to Text and changed it to Number, but the problem remains the same. Any other thoughts about what might be causing the numbers to show up next to each other instead of being added together in some cases?
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Maybe you should post the file, so we can have a look for ourselves.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Are you getting and JavaScript console errors?

Have you checked the field calculation order?

George Kaiser

ForeverSun
Registered: Dec 4 2009
Posts: 8
I am not getting any JavaScript console errors and as far as field calculation order the total number of packages calculation appears last.

By the way, where can I post the file?
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You can post files via acrobat.com

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

ForeverSun
Registered: Dec 4 2009
Posts: 8
I finally found a way to make the total number of packages field work properly. The code is as follows in case anyone runs into the same problem:

event.value = (this.getField("# of packages IM").value * 1+ this.getField("# of packages IM w 1c").value * 1 + this.getField("# of packages IM w 4c").value * 1 + this.getField("# of packages IG").value * 1 + this.getField("# of packages IG w 1c").value * 1 + this.getField("# of packages IG w 4c").value * 1 + this.getField("# of packages Pro").value * 1 + this.getField("# of packages Pro T").value * 1 + this.getField("# of packages BC").value * 1 + this.getField("# of packages M").value * 1)

For some reason, multiplying all of the values by 1 forces an addition calculation to be made in all cases instead of sometimes adding them and sometimes displaying them next to each other. I'm curious why this is the case, but happy all the same. Thank you everyone for you help!
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Multiplying by 1 forces JavaScript to treat the value as a number and not a text string. JavaScript tries to assume if you are processing a number or a text string. The '+' operator can act as the addition operator or string concatenation operator for strings.

Posting an example of the input and result or the file might have resolved this issue sooner.

You can also use the 'Number()' constrictor.

George Kaiser

ForeverSun
Registered: Dec 4 2009
Posts: 8
Thank you for the explanation and 'Number()' constrictor tip. I figured the problem had to do with the data being interpreted as a text string and thought multiplying by 1 would be a good way to force a calculation without changing the result. Appreciate your help!