2 Replies Latest reply on Jun 14, 2009 10:08 AM by gkaiseril

# Major javascript problem

Hey everyone, I'm very amateur with javascript, so please bare with me.

I have a form with several text fields A,B,C,D,E,F,G,H,I,J,K,L. I have a text field (Z) that needs to make the following calculation:

Z = (A x (B/100)) + (C x (D/100)) + (E x (F/100)) + (G x (H/100)) + (I x (J/100)) + (K x (L/100))

But here's my issue; There may be some fields that will be blank, so I need Z to calculate all the fields that have a value.

How can this be done?

Thanks so much.

• ###### 1. Re: Major javascript problem

There shouldn't be any problem; any fields that are empty should evaluate to 0 so that portion of your equation would not be added because it would be 0. e.g. - If G is empty and H was 20, then (G x (H/100)) = 0 x 20/100 = 0.  If H is empty and G was 20, then (G x (H/100)) = 20 x 0/100 = 0. All of the other values would be added together if they all contained values. (This is assuming you have set the fields to number entry and won't have any non-numeric characters)

• ###### 2. Re: Major javascript problem

You should acquaint yourself with the mathematical operators and ordering of execution so you can correctly write your code and simplify the bracketing.

The multiplication operator is "*".

If you use the 'Simplified field notation' in AcroForms you can use the following script:

(A * (B / 100)) + (C *( D / 100)) + (E * (F / 100)) + (G * (H  / 100)) + (I * (J / 100)) + (K * (L / 100))

And because the execution order of the operators is to perform the multiplication and division before addition and subtraction and the associative law of multiplication and division, you do not even need the parenthesis for your situation.

If you were to write a 'Custom calculation script' one would need to use the "Number()' condtrictor to convert null values or empty fields to a numeric zero and not treat them as a character string. You can also use the 'isNaN()' function to test if a field's value is Not a Numeric value. Also if you use the number format for you input fields, you will have the entry values restricted to numeric values only and you can use the 'Validation' tab to restrict the range of numbers that can be entered.