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

How to do an if then statement on a PDF form

Community Beginner ,
Jun 13, 2016 Jun 13, 2016

I am trying to do an if then statement on a PDF form.

The field is a calculated field A*B=C.  If C<10 then C=10, else C=A*B

How do I write the is Adobe Pro on a PDF form?  I have gone into properties, calculated field and nothing I have tried works.

TOPICS
PDF forms
121.4K
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
LEGEND ,
Jun 13, 2016 Jun 13, 2016

Each field is a separate object, so if you have 3 fields like "A", "B", and "C" each is a unique object has a unique value. Each field has to be accessed individually, You need to get the value for field "A" in one statement, the value of field "B" in another statement.

var C = this.getField( "A").value * this.getField("B").value;

if( C < 10 ) event.value = 10;

else event.value = C;

You might see it more clearly with some code like:

var A = this.getField( "A").value; // the value of field A;

var B = this.getField( "B").value; // the value of field B;

var C = A * B; // the value of field A times the value of field B;

if( C < 10 ) {

event.value = 10; // value is 10 when product less than 10;

} else [{

event.value = C; // else value is the product;

}

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
New Here ,
Dec 20, 2018 Dec 20, 2018

Thanks for getting back to me on this and providing additional tips. 

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 ,
Nov 06, 2017 Nov 06, 2017

I have a similar if statement to this one.

but I cant manage it to work.

i have text field A,

the text field B would be dependent of the number to be input on field A.

if A <=12

field B = 1

if A >=13 && <= 24

field B = 2

if A>= 25 && <= 40

field B = 3

please help on what is the correct script for this.

i have use this script.

var B = this.getfield("A");

if (A <=12 && >=2)

B = 1;

if (A <=24 && >=13)

B = 2;

if (A <=36 && >=25)

B = 3;

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 ,
Nov 07, 2017 Nov 07, 2017

Why are you placing the value of field A in a variable called B? That's really confusing.

Use this code as the custom calculation script of the B field:

var A = Number(this.getField("A").value);

var B = "";

if (A<=12) B = 1;

if (A>=13 && A<=24) B = 2;

if (A>=25 && A<=40) B = 3; // Is it 36 or 40? What about if it's higher than 40?

event.value = B;

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 ,
Nov 07, 2017 Nov 07, 2017

it was 40.

if it exceeds 40, it would not accept. because i set the limit to 1-40 only.

thank you very much

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 ,
Nov 07, 2017 Nov 07, 2017
  1. var A = Number(this.getField("A").value); 
  2. var B = ""
  3.  
  4. if (A<=12) B = 1
  5. if (A>=13 && A<=24) B = 2
  6. if (A>=25 && A<=40) B = 3; // Is it 36 or 40? What about if it's higher than 40? 
  7.  
  8. event.value = B;

hello,

the answer on textbox field B does not show.

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 ,
Nov 08, 2017 Nov 08, 2017

Change the value of A and then check the JS Console (Ctrl+J) for error messages.

If it still doesn't work, share the file.

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 ,
Jun 20, 2023 Jun 20, 2023

Hi

I'm trying to create a code to change the color of the field based on two variables. 

if v1 => 32 the fields v1 and v2 should use red color

if v1= 32 and v2>00, the fields v1 and v2 should use red color

if v1= 32 and v2=00, the fields v1 and v2 should use black color 

if v1<=31, the fields v1 and v2 should use black color

 

I'm using the code below, someone could help me? 

 

var v1 = Number(this.getField("v1").value);
var v2 = Number(this.getField("v2").value);


if (v1>32) {event.target.textColor = color.red;}

else if (v1=32 && v2>0) {event.target.textColor = color.red;}

else if (v1<32) {event.target.textColor = color.black;}

else if (v1=32 && v2==0) {event.target.textColor = color.black;}

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 ,
Jun 20, 2023 Jun 20, 2023

Use double == when comparing v1 to be equal to 32:

v1==32

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
LEGEND ,
Nov 07, 2017 Nov 07, 2017

JavaScript is an object orientated language, so one first must establish an object or variable to hold an object and then access a property, like value, or use a property of the object. Also the logical AND, &&, require 2 full complete logical statements. You have not defined the object "A" as a variable or an object.

If one opens the JavaScript debugging console in Acrobat, most JavaScript syntax errors will be reported. Also reordering some of the statements in a more logical progressing arrangement might highlight some logical errors.

// custom calculation script for field B;

try {

var A = this.getField("A1");

var B = 0;

if (A.value  >= 2  && A.value <= 12)

B  = 1;

if (A.value >= 13  && A.value <= 24)

B = 2;

if (A.value  >= 25 &&  A.value <= 36)

B = 3;

event.value = B;

} catch(e) {

app.alert(String(e));

console.show();

console.println(String(e))

}

You state your value will not exceed 40 but you do not cover the situation when the value is between 37 and 40 and you do not test for a value below 2.

You might also want to add some validation action to field A.

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 ,
Mar 13, 2020 Mar 13, 2020

I'm trying to do a fillable Order Form with tiered pricing on variable quantities. Not going to lie... I'm pretty good at figuring out simple code, but know nothing about JavaScript.

 

After reading through this thread, this is what I've come up with so far, but I keep getting a syntax error so I'm not even sure if this is right or not:

 

Total Item 1 = Var Quantity * Var Price

Var Quantity = this.getField(”Q1”).value; or this.getField(Q”2”).value; or this.getField(”Q3”).value;
or this.getField(”Q4”).value; or this.getField(”Q5”).value); or this.getField(”Q6”).value

Var Price = this.getField(”T1”).value; or this.getField(”T2”).value; or this.getField(”T3”).value;
or this.getField(”T4”).value; or this.getField(”T5”); or this.getField(”T6”).value

var quantity = this.getField("Q1").value;
var price = this.getField("Total Item 1").value;
event.rc = (T1=="")&&(Q1=="");
event.readonly = event.rc;
event.value = T1*Q1; else
var price=this.getField("TotalItem1").value;
var quantity = this.getField("Quantity").value;
event.rc = (T2=="")&&(Q2=="");
event.readonly = event.rc;
event.value = T2*Q2; else
var price = this.getField("TotalItem1").value;
var quantity = this.getField("Quantity").value;
event.rc = (T3=="")&&(Q3=="");
event.readonly = event.rc;
event.value = T3*Q3; else
var price = this.getField("TotalItem1").value;
var quantity = this.getField("Quantity").value;
event.rc = (T4=="")&&(Q4=="");
event.readonly = event.rc;
event.value = T4*Q4; else
var price = this.getField("TotalItem1").value;
var quantity = this.getField("Quantity").value;
event.rc = (T5=="")&&(Q5=="");
event.readonly = event.rc;
event.value = T5*Q5; else
var price = this.getField("TotalItem1").value;
var quantity = this.getField("Quantity").value;
event.rc = (T6=="")&&(Q6=="");
event.readonly = event.rc;
event.value = T6*Q6;

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 ,
Mar 13, 2020 Mar 13, 2020

It's not surprising you can't find the syntax error. This code is on the big messy size. I would suggest formatting it so it can be read. And also posting this to s new thread. 

 

There are a couple of obvious issues. You are missing the the "if" block brackets and the structure is incorrect. 

Before writing something this large, you should create a test with minimum code. That way you can figure out these types of issues in simpler, easier to handle setting.  

 

Here's an article on using the "if" statement:

https://www.acrobatusers.com/tutorials/conditional-execution/

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 ,
Mar 13, 2020 Mar 13, 2020

Thank you for your response, Mr. Parker.

Unfortunately, I do not know how to start a new thread.

I think the whole thing is a big ugly mess. Who would have thought that a simple thing could get so entirely complicated?

Basically, I have an order form that has tiered pricing on all items. I want the customer to fill in the Quantity and then I want the total to auto generate based on the quantity entered multiplied by the corresponding tier price. Quantities entered MUST match one of the tiers. In other words, if the customer wanted to order less than 500, then they have to get a custom quote. Same holds true for values in between tiers or over 5000.

 

 

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 ,
Mar 13, 2020 Mar 13, 2020

Well, you have two ways to go on this. 

1) Learn some proper coding and do it yourself. (spend time) 

2) Hire someone to do it. (spend money)

 

I can help with either of these approaches. 

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
Explorer ,
Jun 23, 2020 Jun 23, 2020

Hey, sorry to bother with a different question.

I'm trying to make 2 boxes. If someone puts a number between 1-20 in the first box, another number appears in the second. So if they put a 4, they get a -2. If they put a 14, they get a 2. I need to be able to control the output number, because its not based on a math calculation. 

Would you know how to do this?

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 ,
Jun 24, 2020 Jun 24, 2020

The first thing to do is to write out the exact conditions that result in all the different outputs. A table is a good way to do this.  It needs to include all input conditions. Then you can write an "if" block using logical conditions to test the inputs and return the appropiate result. 

Did you read the article linked above? Do you have any programing experience?

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 ,
Jul 07, 2020 Jul 07, 2020

event.value=(7000+(this.getField("field.nam").value-500000)*0.025) but I want to add a if ("field.nam") == 0 or is less than 100,000 to leave value blank 

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 ,
Jul 07, 2020 Jul 07, 2020

Please post your question, with a clear description of the issue to a new thread.

 

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