Skip to main content
Participant
August 23, 2017
Answered

Javascript calculation after values entered.

  • August 23, 2017
  • 1 reply
  • 1382 views

I currently have the following java script in a adobe pdf to perform calculations.  One of the problems I am running into is the form is performing the calculations live.  I do not want the calculations to be performed till values are entered.  So currently the 3 text boxes are "Neck1", "Neck2" and "Neck3".  Once values are entered into these boxes I would like to perform the average calculation.  Any help would be appreciated.  I am not experience with JavaScript so please be very detailed with answers.

// Custom calculation script (function () {

// Get the field values, as numbers

var v1 = +getField("Neck1").value;

var v2 = +getField("Neck2").value;

var v3 = +getField ("Neck3").value;

var nTotal = (v1 + v2 + v3)

var nAverage = (nTotal / 3)

var nRounded = Math.round(nAverage / .5)*.5;

event.value = nRounded; })();

This topic has been closed for replies.
Correct answer try67

Use this code:

(function () {

    var s1 = getField("Neck1").valueAsString;

    var s2 = getField("Neck2").valueAsString;

    var s3 = getField("Neck3").valueAsString;

    if (s1!="" && s2!="" && s3!="") {

        var v1 = +s1;

        var v2 = +s2;

        var v3 = +s3;

        var nTotal = (v1 + v2 + v3)

        var nAverage = (nTotal / 3)

        var nRounded = Math.round(nAverage / .5)*.5;

        event.value = nRounded;

    } else event.value = "";

      

})();

1 reply

try67
Community Expert
Community Expert
August 23, 2017

Do you mean that you don't want the calculation to occur until all three fields are non-empty?

Walb0244Author
Participant
August 23, 2017

Yes.  That is correct.  I do not want the calculation to occur until all three fields are non-empty.

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
August 23, 2017

Use this code:

(function () {

    var s1 = getField("Neck1").valueAsString;

    var s2 = getField("Neck2").valueAsString;

    var s3 = getField("Neck3").valueAsString;

    if (s1!="" && s2!="" && s3!="") {

        var v1 = +s1;

        var v2 = +s2;

        var v3 = +s3;

        var nTotal = (v1 + v2 + v3)

        var nAverage = (nTotal / 3)

        var nRounded = Math.round(nAverage / .5)*.5;

        event.value = nRounded;

    } else event.value = "";

      

})();