These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

If then Statements for a Javascript in PDF form

gaffkea
Registered: Oct 28 2008
Posts: 11

I have created a form that I need to implement an If Then statement. Basically if the answer to one field is less than 2.99 then I need a particular field to return an answer of 30.

Basically If Field A < 2.99 then Field B=30
If Field A >3.0 but less than 9.99 then Vield B =16
If Field A >10 but less than 29.99 then Field B = 11
If Field A >30 but less than >59.99 then Field B =10
If Field A is great than 60 then Field B = 8

Is their a way that this can be incorporated as a formula?

Thank you
Angie

My Product Information:
Acrobat Pro 6.0.6, Windows
maxwyss
Registered: Jul 25 2006
Posts: 255
This is going to be a series of nested if ... else statements.

You may have to do a careful analysis in order to get the logic right, but then, it is pretty simple and looks like this:

if (condition a) {
do action 1
} else {
if (condition b) {
do action 2
} else {
... etc.
}
}

Hint: in the above example, it would make most sense to test for the greatest value first, and then test in descending order...


Hope this can help.

Max.
gaffkea
Registered: Oct 28 2008
Posts: 11
This kind of makes sense but can I use greater or lessthan symbols in the formula?

Such as: if (Field A)<2.99{Field B =30}else......I guess I am still not sure how to structure this formula to receive the correct result in Field B

Angie
Runolfr
Registered: Oct 1 2008
Posts: 119
You should be able to use all the basic logical operators.

>= greater than or equal
<= less than or equal
== equal
!= not equal
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
The comparative operators are the same as for JavaScript 1.5 with ECMA-262 compatability.

Equal (==) Returns true if the operands are equal. If the two operands are not of the same type, JavaScript
attempts to convert the operands to an appropriate type for the comparison.

Examples:
3 == var1
"3" == var1
3 == '3'

Not equal (!=) Returns true if the operands are not equal. If the two operands are not of the same type,
JavaScript attempts to convert the operands to an appropriate type for the comparison.

Examples:
var1 != 4
var1 != "3"

Strict equal (===) Returns true if the operands are equal and of the same type.

Examples:
3 === var1

Strict not equal (!==) Returns true if the operands are not equal and/or not of the same type.

Examples:

var1 !== "3"

3 !== '3'

Greater than (>) Returns true if the left operand is greater than the right operand.Examples:
var2 > var1Greater than or equal (>=) Returns true if the left operand is greater than or equal to the right operand.Examples:
var2 >= var1
var1 >= 3Less than (<) Returns true if the left operand is less than the right operand.Examples:
var1 < var2Less than or equal (<=) Returns true if the left operand is less than or equal to the right operand.Examples:
var1 <= var2
var2 <= 51 These examples assume that var1 has been assigned the value 3 and var2 has been
assigned the value 4.

You can obtain more information about JavaScript at [url=https://developer.mozilla.org/En/JavaScript]Mozilla Org JavaScript MDC[/url].

George Kaiser

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Here's a more concrete example of what you'll need to do:

// Get value of field A, as a numbervar v1 = +getField("Text1").value; // Get a reference to field Bvar f2 = getField("Text2"); if (v1 < 3 ) f2.value = 30;if (v1 >= 3 && v1 < 10) f2.value = 16;if (v1 >= 10 && v1 < 30) f2.value = 11;if (v1 >= 30 && v1 < 60) f2.value = 10;if (v1 >= 60) f2.value = 8;

Replace the field names in the code above with your field names.

This could be made slightly more efficient using "else" clauses so that every test is not always done, but this makes it a bit more clear.

Also, the code might be different depending on where you want to place this code. For example, if this were to be a custom calculation script for field B, you would set the value of event.value rather than f2.value.

George
gaffkea
Registered: Oct 28 2008
Posts: 11
Now this is starting to make more sense to me. Here is a question for you: would you recommend to have this as a custom calculation or a calculation that is separate that will return the value in Field B

Would you recommend using an else clause?

Thanks again for your assistance.

Angie
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
To give you advice on how this would be best implemented, it would help to know more about how the form is set up. Describe the purpose of the form, how it will be used, and what this purpose of the B field in particular.

In general though, I avoid using custom calculation scripts and would prefer to place this as a custom Validation script for field A. But the code would have to be revised a bit if you choose this approach.

As for adding else clauses, it probably wouldn't make any noticeable difference in this case. You might save a few milliseconds in execution time, some of the time. If there will be a large number of cases though, it may begin to make more sense, and as Max indicated, place the more likely cases near the beginning, which would prevent the tests for the least likely cases from taking place unnecessarily.

George
gaffkea
Registered: Oct 28 2008
Posts: 11
I tell you what I am going to email you a section of the form that I am working on because it is a little challenging trying to describe.

thanks
Angie
gaffkea
Registered: Oct 28 2008
Posts: 11
Basically the form is designed as a pricing tool for our project managers. For this particular section (formula needed) the project manager would place the amount of Sq Ft to base calculation from. Depending on the number the project manager places in this field the price per square foot will change. The hight the number the lower the price per sq foot is.

Basically I want the project manager to put in a value and the form/ calculations will work behind the scenes and will provide them with the proper dollar amount.

I also have a field that will do a simple calculation taking the number of square feet multiplied by the value that was put in ????? field.

Does this help?

Angie
gaffkea
Registered: Oct 28 2008
Posts: 11
okay I am getting closer to getting this thing to work. Right now I have this as a custom calculation field in the field I am looking for the return answer. As of right now it only returns the number 30 no matter what var v1 actually is.

Below is the code I am using in this field. Am I missing something in order for it to perform the other calculations if var1 is a different number other than 3 and lower?

var v1 = this.getField("GFx Sqft area");
var f2 = getField("GFX Cost sq ft");

if (v1 < 3) f2.value = 30;
if (v1 >= 3 && v1 < 10) f2.value=16;
if (v1 >= 10 && v1 <30) f2.value = 11;
if (v1 >= 30 && v1 < 60) f2.value = 10;
if (v1 >= 60) f2.value = 8;Basically in this form I am going out to a field to obtain the response and based on the response this particular field will provide the appropriate number.

So if someone puts in 15 in the field the proper number to be placed in this field should be 11.

Any thoughs on what I am missing?
Angie
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
For a custom calculation script, it should be:

var v1 = +getField("GFx Sqft area").value; if (v1 < 3)  event.value = 30;if (v1 >= 3 && v1 < 10)  event.value=16;if (v1 >= 10 && v1 <30)  event.value = 11;if (v1 >= 30 && v1 < 60)  event.value = 10;if (v1 >= 60)  event.value = 8;

If it were a custom Validation script for the data entry field, the correct code would be:

var v1 = +event.value;var f2 = getField("GFX Cost sq ft"); if (v1 < 3)  f2.value = 30;if (v1 >= 3 && v1 < 10)  f2.value=16;if (v1 >= 10 && v1 <30)  f2.value = 11;if (v1 >= 30 && v1 < 60)  f2.value = 10;if (v1 >= 60)  f2.value = 8;

I'd recommend the second approach. Note that this probably doesn't do what you want if the data entry field is blank.

George
cwebb
Registered: Nov 3 2008
Posts: 1
I am trying to do the same thing as gaffkea; I am trying to have my users enter a number in a field and then the corresponding rating show in the next field. As of now, no matter what value is entered in the first field, "C" shows up in the second field.

Here is my code:

var v1 = +event.value;
var f2 = getField("Rating");

if (v1 = 4) f2.value = "A";
if (v1 = 3) f2.value="B;
if (v1 = 2) f2.value = "C;
if (v1 = 1) f2.value = "D;

It is a custom validation code in the data entry field (Rating).
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
You need to use an equivalence operator (== or ===), not the assignment operator (=), and also there are a few missing quotes:

if (v1 === 4)  f2.value = "A";if (v1 === 3)  f2.value = "B";if (v1 === 2)  f2.value = "C";if (v1 === 1)  f2.value = "D";

George