Search code examples
javascriptadobeacrobat

Adobe Acrobate Javascript Met/Not Met


I have a PDF and what I want to do is that if the sum equals 3 (meaning all criteria are met), then I want "Met" put into a text box, if it doesn't equal 3, then "Not Met" goes into the text box. I have this code, but for some reason it's not working.

var z = this.getField("Score").value; if (z == 3) { event.value = "Met"; } else event.value = "Not Met";

Sometimes with the sum totals 3, it won't always put "Met" in the text box.


Solution

  • The code you have should be working assuming you put it in the custom calculation script of the target field. However, it would fail if any of the inputs to "Score" were blank or filled with non-numbers and you can't always count on Acrobat returning a number when fields are being calculated that may not be formatted as numbers. One thing to note is that blank fields in PDF are not null, they are empty strings so it's best practice to force a field value to a number if you really need a number. You didn't supply the calculation into "Score" so I'm thinking that's where the real problem lies. That said, I made a minor adjustment to your code to check for NaN in "Score".

    var z = parseInt(this.getField("Score").value); 
    if (!isNaN(z) && z == 3) 
        { event.value = "Met"; } 
    else {
        event.value = "Not Met";
    }