Skip to main content
Participating Frequently
April 24, 2025
Answered

Need help writing an "if/then" script in a fillable pdf.

  • April 24, 2025
  • 4 replies
  • 1735 views

So, here's my problem. I have for different Variables var A, var B, var C and var D, with an output box. I am trying to have it where the user inputs a number in one var and it appears on the output box, so if the user inputs 2 in var A and only on var A then it will show up in the ouput box, and the same for the other variables. I've tried this script but was only able to make it work for var A and var B, var C and var D did not work. thanks for the help.

var A = this.getField("VR1").value

var B = this.getField("VR2").value

var C = this.getField("VR3").value

var D = this.getField("VR4").value

if (B==0) event.value= ((A));

if (A==0) event.value= ((B));

if (D==0) event.value= ((C));

if (C==0) event.value= ((D))

 

Correct answer Thom Parker

If all the fields are blank, except one,  then adding them together (i.e., aggregating them), will result in a value that matches the filled field. 

var A = this.getField("VR1").value;
var B = this.getField("VR2").value;
var C = this.getField("VR3").value;
var D = this.getField("VR4").value;
event.value = A + B + C + D;

 

However, if more then one field is filled, then you need a strategy to prioritize which one is selected for the output field. 

Or, if you want to prevent more than one from being filled, then you need a strategy to clear the other fields when a value is entered into one of them.

It's your choice. 

 

 

4 replies

JR Boulay
Community Expert
Community Expert
April 27, 2025

[MOVED TO THE ACROBAT DISCUSSIONS]

Acrobate du PDF, InDesigner et Photoshopographe
Nesa Nurani
Community Expert
Community Expert
April 25, 2025

If only one field will be filled you don't need script for that, you can use built in calculation, under 'Calculate' tab select 'Value is the', leave as sum(+) and click on 'Pick' to select your fields.

Bernd Alheit
Community Expert
Community Expert
April 24, 2025

You can use:

event.value = A + B + C + D;

hans_4580Author
Participating Frequently
April 24, 2025

let me state that I am not a programer and your help is appreciated. I tried to use this script, but was only able to make the firs two lines work.

var A = this.getField("VR1").value

var B = this.getField("VR2").value

var C = this.getField("VR3").value

var D = this.getField("VR4").value

if (B==0) event.value= ((A));

if (A==0) event.value= ((B));

if (D==0) event.value= ((C));

if (C==0) event.value= ((D))

how and where would I use your suggestion? thanks

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
April 24, 2025

If all the fields are blank, except one,  then adding them together (i.e., aggregating them), will result in a value that matches the filled field. 

var A = this.getField("VR1").value;
var B = this.getField("VR2").value;
var C = this.getField("VR3").value;
var D = this.getField("VR4").value;
event.value = A + B + C + D;

 

However, if more then one field is filled, then you need a strategy to prioritize which one is selected for the output field. 

Or, if you want to prevent more than one from being filled, then you need a strategy to clear the other fields when a value is entered into one of them.

It's your choice. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
April 24, 2025

Your description is not very clear, to me at least. What should happen if the user fills in more than one field? A & C? Or A & B? Or A & C & D? Or all four? etc.

hans_4580Author
Participating Frequently
April 24, 2025
so, I have four boxes that user has a choice to enter a value, but only one box is allow to have a value entered, and I need that value to be shown in the output box. e.i.
box A = 2
box B = 0
box C = 0
box D = 0
output box = 2
 
hope that helps. thank you.
hans_4580Author
Participating Frequently
April 24, 2025

So, if all boxes are 0 except for box A, and the user enters a number into box C, then A should be set to zero? So all boxes except one contain a zero?  

 

 

 


No, the users know that only once box will have to have a value, each one of those 4 boxes are for a specific task, and they will only do one of those task per form. So, if they perform task A then only box A will have a value, and that value should carry ou to the output box.