Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Apr 24, 2025 Apr 24, 2025

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

 

408
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Apr 24, 2025 Apr 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2025 Apr 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 24, 2025 Apr 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2025 Apr 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?  

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 24, 2025 Apr 24, 2025

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2025 Apr 24, 2025

Use Bernd's scrpt.  Since you know that users will only enter a value into one field, there is no need to do any checking.  Just add them all up. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 24, 2025 Apr 24, 2025

so, what does the Bernd's script look like for my scenario. Thanks.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2025 Apr 24, 2025

You can't trust that people won't fill in multiple values. You have to actively prevent them from doing so, either by resetting the other fields, or by disabling them when a value is entered into one of them.

If you do that then all you would need to do is aggregate the four values into a single string (replace "0.0" with a blank string), to get what you asked for.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2025 Apr 24, 2025

You can use:

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 24, 2025 Apr 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2025 Apr 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 25, 2025 Apr 25, 2025

that worked like a charm, thank you for your help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2025 Apr 24, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 27, 2025 Apr 27, 2025
LATEST

[MOVED TO THE ACROBAT DISCUSSIONS]


Acrobate du PDF, InDesigner et Photoshoptographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines