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

If Field=0 and Other Field is Blank, do not show zero

SandieB83
Registered: Nov 4 2010
Posts: 10
Answered

I have an inventory form which includes a Description field and then calculations for adding and subtracting to get a total inventory for a given item. As I have it calculating the total right now, it always displays zero whether the calculations = 0 or if nothing was ever entered.
 
What I would like, is for the Total to be blank if the Description of the item is empty.
 
I have read many examples on the forum and can't get any to work. Currently, I have the following code in the Custom Validation Script. It worked when I was just checking for event.value ==0 but when I added the check for Description it doesn't work.
 
if ((event.value == 0) && (this.getfield("DescriptionRow1").value.length == 0)) event.value = "";
 
Can anyone help me please? I am very new to JavaScript.
 
(I am using Acrobat version 9.4.0)

My Product Information:
Acrobat Pro, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Try changing this to:

if ((event.value == 0) && (getField("DescriptionRow1").valueAsString === "")) event.value = "";
SandieB83
Registered: Nov 4 2010
Posts: 10
George,

Thank you for your quick reply. I tried that before but tried it again at your suggestion by copying and pasting exactly what you have and the field is still displaying a zero. Any other ideas?

Just to check that I am doing things correctly:
I am running Adobe Acrobat 9.4.0. This is a calculated text field. If I reset the form so all fields are cleared, it shows a zero value due to being a calculated field. I would like it to show blank when there is nothing to calculate, so I am putting this JS code in the "Run custom validation script" box on the "Validate" tab in the "Text Field Properties" dialogue box.

What am I missing?

Sandie
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
This really should be implemented in a calculation script only, without a validation script as well. What do you currently have for the calculation script?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Are you getting any errors in the JavaScirpt console?

Use the +J keys to open the console.Make sure you are spelling, capitalizing, and including all punctuation for the form field names correctly.

George Kaiser

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
The reason you should implement this as a calculation script only is because the calculated field's Validate event does not get triggered when the DescriptionRow1 text field changes.
SandieB83
Registered: Nov 4 2010
Posts: 10
Ok. Yes. I am getting an error.

This is my script:
if ((event.value == 0) && (getField("DescriptionRow1").valueAsString === "")) event.value = "";When I click my reset form button, I get all of these errors:

TypeError: getField("DescriptionRow1") is null
1:Field:Validate
TypeError: getField("DescriptionRow1") is null
1:Field:Validate
TypeError: getField("DescriptionRow1") is null
1:Field:Validate
TypeError: getField("DescriptionRow1") is null
1:Field:Validate
InvalidSetError: Set not possible, invalid or unknown.
Event.value:1:Field Year1RO:Calculate

But I'm not sure how to fix it.

SandieB83
Registered: Nov 4 2010
Posts: 10
I am calculating using the basic calculation of "Value is the sum of the following fields:" NegTotDed.1, TotalRow1 in the Calculate tab.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Those errors indicate that there is not a field named "DescriptionRow1" present. It it named something else?

Try the following as the field's custom Calculate script, but make sure the field names match your form.

// Custom Calculate script
(function () {

// Get field values as numbers and calculate sum
var v1 = +getField("NegTotDed.1").value;
var v2 = +getField("TotalRow1").value;
var sum = v1 + v2;

// Set this field's value
if (getField("DescriptionRow1").valueAsString === "") {
event.value = "";
} else {
event.value = sum;
}

})();

Do you want the field to be blank if the sum is zero, whether the description field has a value or not?

Edit: I corrected a typo in the code above.
SandieB83
Registered: Nov 4 2010
Posts: 10
Can you explain to me what the + in front of getField means?
Also, what does ++ mean in a calculation?
As well as, the difference between =, ==, and ===?

Thanks.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
The "+" in front of the getField forces the returned value to be treated as a number string and not a character string. The "+" operator is not only the addition operator but also the concatenation operator.

The "++" operator is the increment operator.

"=" is the set operator
"==" is the equality operator
"===" is the strict equal operator

JavaScript Reference Scroll down to the operators section.

George Kaiser

SandieB83
Registered: Nov 4 2010
Posts: 10
Accepted Answer
Piecing together information from all of the above posts, I ended up with the following code in the Custom Calculation Script on the the Calculate Tab. (One of my problems was case-sensitivity in the field name. Thank you.)

event.value = +getField("NegTotDed.1").value + +getField("TotalRow1").value;
if ((event.value == 0) && (getField("DESCRIPTIONRow1").valueAsString === "")) event.value = "";Thank you so much for your help.

P.S. To answer George Johnson's question above, yes I want "zero" to show it the Description field is not blank because it is an inventory form. So it is possible to have an item that had inventory added or subtracted during the year but ended with an inventory of zero. This needs to show as zero not blank. I want it blank if there is not an item description. Thanks for the question.