Skip to main content
Participating Frequently
May 10, 2019
Answered

If/Else Statement from text to numbers in PDF Form

  • May 10, 2019
  • 2 replies
  • 4378 views

Hello, I have been reading few answers in this forum but none is about my case. I'm trying to add an IF / ELSE statement to a quote form in PDF but is not working and obviously I'm writing a wrong code.

I have a drop list called "STATE" and another text field called "FL_CALC". Now my question is how can I get a numerical result result in "FL_CALC" field depending the selection made in "STATE"  field?

All that I need is: that number "145" appear in "FL_CALC" field when you choose FL state in "STATE" field... if you choose any other state, then in "FL_CALC" appear 450. Is this possible?

This topic has been closed for replies.
Correct answer try67

Sure. Use this code as the custom calculation script of "FL_CALC":

event.value = (this.getField("STATE").valueAsString=="FL") ? "145" :  "450";

2 replies

Karl Heinz  Kremer
Community Expert
May 10, 2019

I would recommend that you learn how JavaScript works. It's not overly complex, and you can probably learn a lot about it in just an afternoon. A while ago I wrote a blog post about how to learn to program in JavaScript for Acrobat: Learning to Program JavaScript for Adobe Acrobat - KHKonsulting LLC

In general, an if else statement looks like this:

if (somethingIsTrue) {

  // .... if block

}

else {

  // .... else block

}

If "somethingIsTrue" is true, then the "if block" gets executed, otherwise the "else block".

So, in your example, the user picks the "STATE", and when STATE is set to "FL", you want FL_CALC to be 145, and otherwise you want FL_CALC to be set to 450:

if (this.getField("STATE").value == "FL") {

  this.getField("FL_CALC").value = 145;

}

else {

  this.getField("FL_CALC").value = 450;

}

This does not contain any error checking (e.g. what happens if the field "STATE" or "FL_CALC" does not exist?), so you may want to look into some exception handling.

The code as I've described it will work from e.g. a global calculation script. If you want to do a calculation script, you will need to set the return value for the event handler: You want to calculate FL_CALC based on the "STATE" field, so you will need to create a custom caulcation script for the field FL_CALC. There are more elegant ways to do this, but to keep it easy to understand, I would recommend you use something like  this:

if (this.getField("STATE").value == "FL") {

   event.value = 145;

}

else {

  event.value = 450;

}

Participating Frequently
May 10, 2019

Thanks Karl... I will give a try tomorrow when I be back to my office.... thanks for taking time to answer me....

try67
try67Correct answer
Community Expert
May 10, 2019

Sure. Use this code as the custom calculation script of "FL_CALC":

event.value = (this.getField("STATE").valueAsString=="FL") ? "145" :  "450";

Participating Frequently
May 11, 2019

Ok, first at all... THANK YOU !!!...works perfect, but....

as you can see in the above pict. the total field created is always shown the value $ 450, (because we set the custom script for "FL_CALC" field in my first question). As we set FL = 145 and every else = 450, this rule include the "--- Select State ---" option as well, but there is a chance to exclude this first option and to set FL = 145, else = 450, and --- Select State --- = 0 ?? everything in the same custom calculation script ?... I prefer that every time that open this PDF or push the CLEAR button, value shown be zero (0), since is the total result for a quote, and doesn't looks good even before to start filling up the form that $ 450 appears and you don't know from where... I been trying but I mess up everything with not result at all.

Thanks again in advance.

try67
Community Expert
May 11, 2019

Yes, it's possible. Use this code:

var f = this.getField("STATE");

if (f.valueAsString == f.defaultValue) {

    event.value = 0;

} else if (f.valueAsString == "FL") {

  event.value = 145;

} else {

  event.value = 450;

}