I have found that one can use the following script for the "Custom validation script" on the "Validate" tab in Acrobat:
// if value is zero replace with null string if(event.value == 0) event.value = "";
Note the form will need to be cleared before this script will take place, as this script will not run until the field in which it is locate is calculated.
If you are writing custom calculation script, then you can add the script to your calculation.
If one uses FormCalc, then a variation of this script can be used to supress zero.
Thanks for the reply. I tried it on 2 calculated fields. The one that was a simple addition worked fine. The one with a custom calculation script didn't work. It was probably because I am new to scripting and didn't incorporate it into the calculation script properly. The custom calculation script is as follows:
// Calculation script (function () {
// Get field values as numbers var numerator = +getField("tot_liq_assets").value; var denominator = +getField("adj_net_worth").value;
// Get field values as numbers var numerator = +getField("tot_liq_assets").value; var denominator = +getField("adj_net_worth").value; var result = numerator / denominator;
Again, thanks for the reply. The last script didn't get rid of the zero of the calculated field. I have 2 calculated fields: "tot_liq_assets" (which is the sum of 10 other fields) and "conc_liq_assets" which is calculated by dividing "tot_liq_assets" by an inputted field value "adj_net_worth".
The first part of the script eliminated an error message that I was getting when trying to input the 10 values (that summed to "tot_liq_assets" ) when no value for "adj_net_worth" was inputted yet.
I don't want to display zero values for "tot_liq_assets" and "conc_liq_assets" that occur before any fields are filled in.
If your field has numeric formatting beyond the decimal point and thousand separator, you can not suppress a zero result. This means any field with a currency symbol or percentage symbol in the displayed format will display a zero result.
If you do not want to show those types of fields, then one needs to adjust the visible/hidden property of the field.
Yes, I have one of the fields formatted as a percentage, so the % sign is automatically added. What if I formatted the field as a number and added the % sign as part of the form. The calculation would the be adjusted to something like: (numerator / denominator) * 100 to yield a percentage. Would this scrip work?
/ Calculation script (function () {
// Get field values as numbers var numerator = +getField("tot_liq_assets").value; var denominator = +getField("adj_net_worth").value; var result = (numerator / denominator) * 100;
What you are suggesting will make the calculation and any calculation that uses that field to behave in a non-expected manner. This can lead to errors and those errors can be very hard to track down.
I mentioned hiding the field.
You can change the 'hidden' or 'display' property of the field. Using this technique maintains the expected computation outcomes and methods. Works in Reader and Acrobat.
// Calculation script (function () { this.getField(event.target.name).display = display.hidden; // Get field values as numbers var denominator = +getField("adj_net_worth").value;// Perform calculation if (denominator !== 0){ var numerator = +getField("tot_liq_assets").value; event.value = numerator / denominator; if (event.value != 0) this.getField(event.target.name).display = display.visible; } return })();Or you can change the format of the field:
( function () { // assume zero result event.value = ""; AFNumber_Format(2, 0, 0, 0, "",false); // Get field values as numbers var denominator = +getField("adj_net_worth").value; // Perform calculation if (denominator !== 0){ var numerator = +getField("tot_liq_assets").value; event.value = (numerator / denominator); AFPercent_Format(2, 0, 0); } return; })();
I have been following this topic closely as I have a similar problem. The basic validation script given on 12/15/10 at 7:49 has worked perfectly for me also as far as not showing zeros in the calculated fields, with 1 exception. There are certain lines on a form where the user needs to be able to manually enter a zero. With the basic validation script, the field will allow the user to enter a zero, but immediately overrides it so that the field is blank. For Example:
Subtract line 6 from line 5. If zero or less, enter "-0-". The validation script was entered as:
//if value is zero replace with null string if(event.value==0)event.value=""; //if value is
I would not use the validation script, but use a custom calculation script for that field. Within the custom script I would test for a value in all of the fields used for the calculation, and if all have a value then I would set the value to zero and if one or more were missing a value then the null string would be the event value.
Thanks so much-you're a great help! Another question please: I have a series of 10 fields w/calculations on alternating fields. When I Enter a # in field 1, same # appears in fields 3, 5, & 7 as each builds off the previous calc. Entering a new # in field 2 should cause field 3 to calculate the diff or if zero or less, I want it to be 0. Instead of changing to zero, the answer remains the same as the # entered in field 1. This is my calc: if (this.getField("Line1").value>this.getField("Line2").value) { event.value=this.getField("Line1").value-this.getField("Line2").value; }else{ event.value==0; } Any ideas? Thanks, Patsy
I have a form that cannot have any zeros in any of the fields until data is entered. The line reads "Subtract Line 2 from Line 1. If zero or less, enter "0" ". The calculation I have is working perfectly, with 1 exception: (1) if Line 1 is > Line 2, it subtracts perfectly; (2) if Line 1 is < Line 2, it inserts a 0; (3) it has no zeros in any of the fields until data is actually entered. The problem is if Line 1 & 2 are =, it won't automatically return a 0. My script is as follows: (function(){ event.value=""; if(this.getField("EstimateDed").value>this.getField("FileStatus").value) { event.value=this.getField("EstimateDed").value-this.getField("FileStatus").value; } if(this.getField("EstimateDed").value
It is pretty hard to figure anything out with a partial script.
You need to add a test to see if line 1 or line 2 has a non-blank value and if one mets that test, then the result of the calculation should be displayed.
For line 3 is the difference between line 1 and line 2, the custom calculation script for line 3 could be:
//establish values for field names for value 1 and value 2 var cLine1 = 'line 1'; // field name 1 var cLine2 = 'line 2'; // field name 2 // get values for line 1 and line 2 var nValue1 = this.getField(cLine1).value; var nValue2 = this.getField(cLine2).value; // by default null the value of the result field event.value = ""; // test for a value in line 1 or line 2 if(nValue1.toString() != "" | nValue2.toString() != "") { // we have a value - compute result event.value = nValue1 - nValue2; }
You only need to change the field names. This also shows that it is possible to write generalized code and since JavaScript supports functions, it is possible to write a callable function to perform this calculation with just providing the field names or values.
I have an additional problem related to this post. It reads: Subtract Line 19 from Line 18. Enter the result, including negative amounts. If your employer cannot accommodate negative balances, enter zero here." The script you provided on 1-18-2011 worked perfectly, with one exception. I need an additional script so that, if a user receives a negative difference, the user can override that with a zero. I'm hoping you can help, George. Thanks, Patsy
I have found that one can use the following script for the "Custom validation script" on the "Validate" tab in Acrobat:
// if value is zero replace with null string
if(event.value == 0) event.value = "";
Note the form will need to be cleared before this script will take place, as this script will not run until the field in which it is locate is calculated.
If you are writing custom calculation script, then you can add the script to your calculation.
If one uses FormCalc, then a variation of this script can be used to supress zero.
George Kaiser