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

conditional execution

schipper
Registered: Dec 3 2008
Posts: 3
Answered

Hi,

I'm a new JavaScript user, trying to do something I thought would be simple.

I have been trying to use conditional execution in a form in Adobe. In a nutshell: The form is a refund form I need to fill out regularly in order to be reimbursed for travel and other research related expenses. There are four fields in which I can insert values (currency type), and there there is never more than one field containing a value in a row.

In addition, there is a column for “exchange rates”, for “Exchange”, and for “Total; the latter two is where I want to use conditional execution using a simple if / else construction. Each field (Exch1 and Tot1) contains the following script, which first defines some variables and retrieves values for these from relevant fields, then calculates the exchange on a cost if I insert the exchange rate; if the field for exchange rates is empty, the script will put nothing in the field. The same process should work for the second field (Tot1).

This is the script:

Rate = this.getField("Rate1").value;
X = this.getField("Exch1").value;
M = this.getField("Meals1").value;
A = this.getField("Hotel1").value;
T = this.getField("Trvl1").value;
O = this.getField("Other1").value;
Tot = M + A + T + O;

if (Rate=="") {
event.value = "";
} else {
event.value = Tot + X; [NOTE: to calculate the exchange rate, this will be Rate * X]
}

From my form (where I’m using some dummy values to test the script) I have the following:
Rate = .05
M = “”
A = $150.58
T = “”
O = “”
The format of the fields whose values are placed in M, A, T, and O is “Number”, with $ sign displayed and 2 decimal places (i.e. these are fields containing currency). The same format applies to “Exch1” and the field in which the formula resides (= Tot1).

Result of Rate * X = $7.53
Result of event.value SHOULD be $150.58 + $7.53 = $158.11 [that is, that’s what I’m expecting]
Instead it is $1.#R

I am just a beginner in JavaScript, and can’t figure out why my little script is doing this. If I remove the formatting from Exch1 and Tot1 the results are equally odd (instead of $1.#R, the result is 150.587.529). It looks like my script is treating these values as text rather than numbers, and is concatening them, rather than adding them.

Am I right? Any suggestions as to how to fix this? I’m not at home, so don’t have my Javascript reference with me. Is there a statement that converts a string to a number?

I read the "Conditional Execution" tutorial, which made clear to me how the basic calculations worked, but it doesn't have anything to say about my little problem as far as I can tell.

Or is the solution much simpler?

Bill Schipper

Bill Schipper
English Dept
Memorial University
St. John's, NL, Canada

My Product Information:
Acrobat Pro 9.3.1, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
The problem is most likely you're doing the addition wrong. You do indeed need to convert the field values to number before attempting numerical addition, otherwise you'll get string concatenation if you happen to have one or more empty fields involved. To explicitily convert a field value to a number, you can simply use the unary + operator:

// Get field value as a number
var v1 = +getField("number1").value;

If the field is empty, v1 will be equal to zero.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You can also use the 'Number()' constrictor to force a null string to any number as a character string to a number.

Rate = this.getField("Rate1").value;X = this.getField("Exch1").value;M = this.getField("Meals1").value;A = this.getField("Hotel1").value;T = this.getField("Trvl1").value;O = this.getField("Other1").value;var Tot = Number(M) + Number(A) + Number(T) + Number(O); if (Rate == "") {event.value = "";} else {event.value = Tot + X;}

George Kaiser