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
  • 4387 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
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
Community Expert
try67Community ExpertCorrect 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 10, 2019

Thanks for your prompt response, unfortunately I already left my office but tomorrow morning I will try... Thanks a lot try67, I have reading a lot of your post and believe me, 2 days ago I knew "CERO" about java script and PDF forms... Today I almost finalize my first form using a lot of your answers.

Thanks...