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

Maximum Percentage

phorsley
Registered: Nov 2 2007
Posts: 35
Answered

HI,
 
Is it possible that when a user types in percentage values into various fields, that all the values in a group of fields when calculated can be no more than 100%.
 
Is it possible to have a warning pop up.
 
thanks
 
Paul Horsley

My Product Information:
Acrobat Pro 9.3.1, Macintosh
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Yes. One hundred percent is 1.00 in a numeric field, so there is room for it to be larger.

Yes. You only need to test the value to see if the value is larger than 1.00 and then you write some JavaScript to display an alert. Acrobat form fields have a validation tab with which one can write a custom JavaScript to validate the value of the field.

George Kaiser

phorsley
Registered: Nov 2 2007
Posts: 35
Sorry George not quite sure what you mean by this, what I need is a calculation of numerous boxes so the total is no more than 100, if the for instance type 25, 25,25,26, a warning box will pop up saying that the total can be no more than 100%.

thanks

Paul
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
If you format the text field as a percentage, then the value is the field is the decimal of the value where 100% us 1.00 but the decimal value is multiplied by 100 and the percentage symbol is add to the display. This method allows for a nice clear display of the value and easy calculation of values to and from the percentages.

You can use JavaScript's 'if(logical statement){// block of code}' statement to execute an 'app.alert()' message when the total is over 100%.

George Kaiser

smit462
Registered: May 20 2006
Posts: 20
If you're just using whole numbers, you just need a custom calculation script in the calculation tab of one of your fields like:

var f1 = this.getField("Text field1").value;
var f2 = this.getField("Text field2").value;
var f3 = this.getField("Text field3").value;
var f4 = this.getField("Text field4").value;
var fT = f1 + f2 + f3 + f4;
if (fT > 100){app.alert("Hey dummy!\r\rYou've exceeded 100%");}

STEVE MITTEL
Senior Forms Designer
Texas Comptroller of Public Accounts

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Just be aware that if you compute the percentage the result will be a decimal, that is the percentage value divided by 100, value and if you want to use the percentage you need to divide the percentage value by 100. Using the percentage format allows one to enter the decimal value from the keyboard or by computation and use the decimal value in a computation without any additional special computation and the user will see the decimal value displayed as a percentage with the percentage sign. This can be a real godsend when working with a large complex form as one does not need to remember to perform the 'special' computation to adjust the percent value to a decimal value. Further an experienced programmer will be expecting the percentage field to be handled in this manner.

George Kaiser

phorsley
Registered: Nov 2 2007
Posts: 35
I used Steve's script he posted, which worked great and alerted you when it was over 100% but
when you deleted a value to bring it under 100% the alert box still came up even thou the value was
under 100%. The thing is I now have 57 fields it has to calculate.
I have created forms before but not with javascript so I'm a bit lost on this.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Sounds like there's a problem with the field calculation order. Make suire that the total field is the last to calculate.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
After carefully looking at Steve's example, I see he does not allow for the known issue of JavaScript using the "+" operator as the arithmetic addition operator and string concatenate and JavaScript automatically determining which operator to use by the assumed type of fields used as the operands. Note that an empty or null string in a field will be treaded as a character string and not the nubmer zero. See , Sum vs. Plus for a working example. You will have to make sure all the values are numbers and not null strings. You can use the 'Number()' consctrictor to force a null value to the numeric value 0.You can use the following script in the custom calculation field or remove the lines with 'event.value' and use the custom validation script.

// array of field names to sum
var aFieldNames = new Array ("Text1", "Text2", "Text3");
// variable for total sum
var nSum = 0;
// variable for value of a field
var nField;
// perform the addition of the fields named in the array
// starting with element 0 to the length of the array, increment by one
for(i = 0; i < aFieldNames.length; i++) {
// get the value of i field name field
nField = this.getField(aFieldNames[i]).value;
// force to number
nField = Number(nField);
// add value to sum
nSum += nField;
} // loop back for next i
// test value sum
if(nSum > 100) {
app.alert({cMsg: "Total of all allocations can not exceed 100%\n\nYour total is " + nSum, nIcon: 1, nTyppe: 0, cTitle: "Validation Error"});
event.value = "";
}
else event.value = nSum;

George Kaiser

smit462
Registered: May 20 2006
Posts: 20
When you remove the number from a field, it doesn't calculate the empty field as a zero. Try this script:

var f1 = this.getField("Text field1").value;
var f2 = this.getField("Text field2").value;
var f3 = this.getField("Text field3").value;
var f4 = this.getField("Text field4").value;
var fT = (1 * f1) + (1 * f2) + (1 * f3) + (1 * f4);
if (fT > 100){app.alert("Hey dummy!\r\rYou've exceeded 100%");}Good luck.

STEVE MITTEL
Senior Forms Designer
Texas Comptroller of Public Accounts

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The use of the 'Number()' constrictor will also accomplish the same result.

I prefer creating an array of field names to process and then call a document level function to perform the calculation on the array of field names. This function can then be called from many calculations and easily modified by changing the array of field names to process.

function Sum(aFieldNames) {
// variable for total sum
var nSum = 0;
// variable for value of a field
var nField;
// perform the addition of the fields named in the array
// starting with element 0 to the length of the array, increment by one
for(i = 0; i < aFieldNames.length; i++) {
// get the value of i field name field
nField = this.getField(aFieldNames[i]).value;
// force to number
nField = Number(nField);
// add value to sum
nSum += nField;
} // loop back for next i
// return the computed sum
return nSum;
} // end of Sum function

Then the custom calculation script becomes:

// field names to sum
var aNames = new Array ("Text1", "Text2", "Text3");
// compute sum
var nTotal = Sum(aNames);
// test sum
if(nTotal > 100) {
app.alert({cMsg: "Total of all allocations can not exceed 100%\n\nYour total is " + nTotal, nIcon: 1, nTyppe: 0, cTitle: "Validation Error"});
event.value = "";
}
else event.value = nTotal;

If there are other fields needing to be summed can use:

event.value = Sum(["Text1", "Text2", "Text3", "Text4", "Text5"]);

See Sum vs. Plus for a working example.

George Kaiser