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

How to Subtract and Add in Form Fields

daveripkoski
Registered: Mar 25 2009
Posts: 6
Answered

I'm having trouble getting addition and subtration to work correctly.

I have a set of 5 fields that need to be added up and the one field that needs to be subtracted to give a total.

(Field 1 + Field 2 + Field 3 + Field 4 + Field 5) - Field 6 = ?

How do I make the final field equal this? With the code I have currently, it's giving me this:

$10,000,025,000, where the answer really should be ($100,000+25,000)- 0 = 125,000.
But instead of giving a sum of 100K + 25K, it's putting the two side by side.

Can anyone please help me out? I would GREATLY appreciate suggestions. Here is what I have currently, which isn't working:

event.value = ( this.getField("FIELD 1").value +
this.getField("FIELD 2").value +
this.getField("FIELD 3").value +
this.getField("FIELD 4").value +
this.getField("FIELD 5").value ) -
this.getField("FIELD 6").value;

if(event.value == 0) event.value = "";

ALSO, is there a way to make the "Text" entered in a form print black... but not the rest of the form print black? - just the text?

My Product Information:
Acrobat Pro 9.1, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
JavaScript uses the "+" operator for arithmetic addition and string concatenation and also is not very strict about enforcing field types like string and numeric. The result is that JavaScript tries to guess what the field type is based on the value and the operator. But in many cases gets this wrong so for your string of "+" operations you may have the first 2 fields added because they are non-blank numbers but since th3 3rd field is blank it is treated as a string and the 3rd and following fields are concatenated. So you need to force JavaScript to use a numeric value and not a string value. This can be done by using the 'Number()' constrictor or multiplying each value by 1.

event.value = Number(this.getField("FIELD 1").value) +Number(this.getField("FIELD 2").value) +Number(this.getField("FIELD 3").value) +Number(this.getField("FIELD 4").value) +Number(this.getField("FIELD 5").value)  -Number(this.getField("FIELD 6").value); if(event.value == 0) event.value = "";

or
event.value = 1 * this.getField("FIELD 1").value +1 * this.getField("FIELD 2").value +1 * this.getField("FIELD 3").value +1 * this.getField("FIELD 4").value +1 * this.getField("FIELD 5").value  -1 *this.getField("FIELD 6").value; if(event.value == 0) event.value = "";

George Kaiser

daveripkoski
Registered: Mar 25 2009
Posts: 6
Thank you SO much! It works perfectly now! I can't thank you enough... I GREATLY appreciate your help! :) -dave
smokn14jo
Registered: Apr 3 2009
Posts: 5
thank you ... this also helped me w/ getting rid a a whole bunch of hidden fields a previous person left on a form I need

THANKS!
mabaiglon
Registered: Jun 11 2009
Posts: 1
I would appreciate some help. I have read all the submissions and so must be doing something wrong, but do not know what. I have three fields - SubTotal, DisAmount and TotalDue. SubTotal and DisAmount are both calculated fields and then TotalDue = SubTotal-DisAmount. I have checked the order and it is correct.

I have tried the simplified code of SubTotal-DisAmount and also more complicated scrips of:
var a = this.getField("SubTotal).value;
var b = this.getField("DisAmount).value;
event.value = a-b;

as well as
var a = this.getField("SubTotal);
var b = this.getField("DisAmount);
event.value = a.value -b .value;

I also tried

event.value = Number(this.getField("SubTotal").value) - Number(this.getField(DisAmount).value);

However, all that happens is that the value in the field doesn't change. If I change one of the other fields, everything calculates correctly except TotalDue which remains fixed. There are no errors that I can find.

I have tried a number of the submissions but none work so I don't know what I am doing wrong, but would appreciate some direction.

Thanks
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
In the both code snippets, you did not enter the trailing quotation mark needed to terminate the character string for the field name character stings.

var a = this.getField("SubTotal").value;var b = this.getField("DisAmount").value;event.value = a - b;

or

var a = this.getField("SubTotal");var b = this.getField("DisAmount");event.value = a.value - b.value;

If you look at Acrobat's JavaScript debugging console, the message "un terminated string literal" indicates JavaScript does not know where a character string ends and the JavaScript code continues.

George Kaiser

craigL
Registered: Jan 28 2011
Posts: 3
gkaiseril, your post is very informative and is exactly what I have been looking for; however, it isn't working for me and I hope you can help.
I have created a form intended to calculate net worth. I have one column for assets, and another for liabilities. Each of these two columns, respectively, has a totals field. To calculate the net worth field, I need to subtract total liabilities from total assets. When I write the formula into the simplified field notation, which is ValueTOTAL ASSETS - TOTAL LIABILITIES, nothing happens. It does not calculate.
Do you have any ideas?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You can not easily use '.' or spaces in field names in the Simplified Field notation. You need a special leading character called the 'Escape' character in JavaScript. If you use the '.' or a space in a field name, you need to precede that character with the "\" character. So the entry for the Simplified Field Notation should be:

TOTAL\ ASSETS - TOTAL\ LIABILITIES


George Kaiser

craigL
Registered: Jan 28 2011
Posts: 3
The field still isn't populating. I'm not doing something correctly, but don't know what.
craigL
Registered: Jan 28 2011
Posts: 3
I figured it out. You were extremely helpful. Thanks so much for your help, George.