Skip to main content
Participating Frequently
Apr 24, 2025 1:53 PM UTC
Answered

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

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))

 

Thom Parker
Thom ParkerCorrect answer
Community Expert
Apr 24, 2025 4:57 PM UTC

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

13 replies

JR Boulay
Community Expert
Community Expert
Apr 27, 2025 8:20 PM UTC

[MOVED TO THE ACROBAT DISCUSSIONS]

Translate
Acrobate du PDF, InDesigner et Photoshopographe
Nesa Nurani
Community Expert
Community Expert
Apr 25, 2025 3:32 AM UTC

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.

Translate
Bernd Alheit
Community Expert
Community Expert
Apr 24, 2025 3:48 PM UTC

You can use:

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

Translate
hans_4580Author
Participating Frequently
Apr 24, 2025 4:17 PM UTC

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

Translate
Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
Apr 24, 2025 4:57 PM UTC

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. 

 

 

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
Apr 24, 2025 2:30 PM UTC

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.

Translate
hans_4580Author
Participating Frequently
Apr 24, 2025 3:12 PM UTC
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.
Translate
Thom Parker
Community Expert
Community Expert
Apr 24, 2025 3:28 PM UTC

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?  

 

 

 

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often