3 Replies Latest reply on May 24, 2011 9:37 PM by George_Johnson

# value to be blank unless calc = zero?

hi all,

I'm stumped on a fillable form PDF where I have a simple calculation, say for example, A + B = C, where A and B are fillable fields, and C automatically calculates what the answer would be.

I would like to find out if there is a way to make C blank unless A and B are zero - in other words, is there some way to make C blank if A and B have not yet been filled in, but once they are filled in, make it show whatever the answer is, even if it's zero? I'm getting a lot of "if event.value == 0" whenever I google for an answer, and really I only want a blank field when other fields are not filled in.... help!

thanks!

• ###### 1. Re: value to be blank unless calc = zero?

Sure there is. Just get the field values of A and B as strings and see if either both are blank, and only set the field value if they are not. The barebones script would look something like:

```(function () {

// Get the field values, as strings
var v1 = getField("A").valueAsString;
var v2 = getField("B").valueAsString;

if (v1 && v2) {
event.value = +v1 + +v2;
} else {
event.value = "";
}

})();
```

This could be simplified to just:

```(function () {

var v1 = getField("A").valueAsString;
var v2 = getfield("B").valueAsString;

event.value = v1 && v2 ? +v1 + +v2 : "";

})();
```

Replace "A" and "B" in the code with the actual names of the input fields.

The +v1 and +v2 converts the strings to numbers so you get numerical addition instead of string concatentation

Edit: corrected typo

• ###### 2. Re: value to be blank unless calc = zero?

thanks for the superfast answer! I spent several hours struggling through this on my own, and was nowhere close to this answer, so this is a huge help, and your code is now in four different places on my PDF using subtraction, multiplication and division variations. Thanks again!

• ###### 3. Re: value to be blank unless calc = zero?

Great. Note that when doing division you should add an additional check to make sure the denominator does not evaluate to zero. For example, if the user simply entered a decimal point, the field would not be blank so the code above would accept the value, but when it got converted to a number for the division, it will result in division by zero, which will result in either Infinity, -Infinity, or NaN (Not a Number, for 0/0).