Copy link to clipboard
Copied
I'm trying to create a form with:
1. a series of numerators and denominators to calculate
2. if the field is left blank, it is not included in the calculation
3. the calculation to be updated in real time, and not wait for each field to have a number in it before it calculates.
This is the only calculation in the form, so changing the calculation order is not used.
In the document javascript, I have
this.calculateNow();
and in the custom calculation script for the field, I have
(function () {
var v1 = +getField("Text1").value;
var v2 = +getField("Text2").value;
var v3 = +getField("Text3").value;
var v4 = +getField("Text4").value;
var v5 = +getField("Text5").value;
var v6 = +getField("Text6").value;
var v7 = +getField("Text7").value;
var v8 = +getField("Text8").value;
var v9 = +getField("Text9").value;
var v10 = +getField("Text10").value;
event.value = (v2 * v4 * v6 * v8 * v10) !== 0 ? ((v1 * v3 * v5 * v7 * v9) / (v2 * v4 * v6 * v8 * v10)) : "";
})();
The issues I'm having:
1. that the calculation does not calculate in real time
2. all fields still have to be entered for the calculation to occur.
3. if zero is entered, the calculation will not run.
Try this code:
...(function () {
event.value = "";
var v1 = this.getField("Text1").valueAsString;
var v2 = this.getField("Text2").valueAsString;
var v3 = this.getField("Text3").valueAsString;
var v4 = this.getField("Text4").valueAsString;
var v5 = this.getField("Text5").valueAsString;
var v6 = this.getField("Text6").valueAsString;
var v7 = this.getField("Text7").valueAsString;
var v8 = this.getField("Text8").valueAsString;
var v9 = this.getField("Text9").valueAsString;
Copy link to clipboard
Copied
You don't need the calculateNow command. The calculation occurs each time the value of any field in the file is changed.
The issue is with your condition. If any of the even fields (2, 4, 6, 8, 10) are empty (or zero), the result will be zero and your field will remain empty.
If you don't want that to happen then you need to better explain how you do want it to work.
Copy link to clipboard
Copied
Thank you for your reply!
You are correct, I would like the field to calculate even if 4, 6, 8, or 10 are empty - even though they are denominators.
Copy link to clipboard
Copied
Try this code:
(function () {
event.value = "";
var v1 = this.getField("Text1").valueAsString;
var v2 = this.getField("Text2").valueAsString;
var v3 = this.getField("Text3").valueAsString;
var v4 = this.getField("Text4").valueAsString;
var v5 = this.getField("Text5").valueAsString;
var v6 = this.getField("Text6").valueAsString;
var v7 = this.getField("Text7").valueAsString;
var v8 = this.getField("Text8").valueAsString;
var v9 = this.getField("Text9").valueAsString;
var v10 = this.getField("Text10").valueAsString;
if (v2=="" && v4=="" && v6=="" && v8=="" && v10=="") return;
var x = 1;
if (v2!="") x*=Number(v2);
if (v4!="") x*=Number(v4);
if (v6!="") x*=Number(v6);
if (v8!="") x*=Number(v8);
if (v10!="") x*=Number(v10);
if (x!=0) {
var y = Number(v1) * Number(v3) * Number(v5) * Number(v7) * Number(v9);
event.value = y/x;
}
})();
Copy link to clipboard
Copied
Thank you so much for your help!
It is throwing an error for a missing ) in parenthetical 27:Field:Calculate, but I don't see where this error is stemming from.
Copy link to clipboard
Copied
Works fine for me... Did you copy the whole code?