In Create a PDF Check Box Scoring Questionnaire in Five Minutes I outlined basic math addition from a series of check box fields by using numbers as their export values and also using Right-click > create multiple copies to create the fields with numbered suffixes in the names (Checkbox.0, Checkbox.1, Checkbox.2, etc.) This technique allows the selection of only one field (Checkbox in this example) in Value is the sum of in the calculation tab of a text field. While the value of an unchecked check box field is "Off", "Off" is treated as zero when you use the Value is the sum of in the calculation tab of the field properties.
Mutual Excusive Check Boxes Using Number Export Values
Sometimes a form is designed with a series of radio buttons or mutually exclusive check boxes so the end user can select a product or feature, and its price will be used in another calculation. Once a radio button in a series is checked all the buttons in the series can’t be unchecked, so check boxes need to be used instead if this functionality is required.
A series of mutually exclusive check boxes will have the same name and different export values. If two or more in the series have the exact same export values, they will both be checked if either one is checked and they will both be unchecked if either one is unchecked. This causes an issue if more than one in the series have same price, or number, which requires the same export value for the math to work properly.
What’s The Solution?
When the export value of the check box is assessed by the application to determine whether its value is identical to other fields with the same name, it uses the string value, or valueAsString. However, if the string is also a number used in a math calculation the program takes the value as a number. A practical example I encourage you to test will make this clearer.
Example
Create a check box field called Price.
Copy it twice for a total of three fields.
Set the export values at 50, 050, and 0050.
Test the fields to confirm they are mutually exclusive.
With the 50 export value box checked, run the following script in the console:
this.getField("Price").valueAsString;
Check the next box and run the script again.
Repeat. The script should have returned the following values: 50, 050, and 0050.
Now check the boxes again in order and run the following script for each check:
this.getField("Price").value;
This time the script should return 50, 50, and 50.
Now you have a series of mutually exclusive check boxes in which the value is 50 if any of them are checked.
Use In a Form
Suppose you have ten options on the form with instructions to "Select One", and each has a price as the check box export value, but some prices are the same (e.g. 10, 20, 25, 25, 25, 35, 45, 50, 50, 100). The export values would be:
10, 20, 25, 025, 0025, 35, 45, 50, 050, 100
Now suppose you have a field Sales Tax, that calculates 10% of the Price field. You would enter the following custom calculation:
if(this.getField("Price").value!="Off")
{event.value=this.getField("Price").value * 0.1;}
else
{event.value=""}
The potential "Off" value needs to be excluded from the calculation to prevent a NaN (Not a Number) value.
Counting Answers
A lot of questionnaire-type forms with check boxes have pro-rated point systems, or average points per answer, both based on the number of questions answered. That means the number of check boxes in these calculations, that have a least one in the series checked, need to be counted to obtain "number of questions answered". When using Right-click > Create multiple copies to create those fields, the field names are given a decimal suffix (.0, .1, .2, etc.) Here’s the process for counting the questions answered (boxes checked):
Define a variable and set its value to zero.
Loop through the check box fields. If the value does not equal "Off", increment the variable by one.
The variable after the loop will represent the number of questions answered.
NOTE: When creating multiple copies the decimals will be 0 to the number of fields minus 1, if you create 1 across and the rest down:
That makes it easy to use the number of fields as the limit in the loop:
var num=0;
for(var i=0; i<10; i++)
{
if(this.getField("Check Box." +i).value!="Off")
{num++}
}
// Now do something with num, which represents the number of boxes checked
If your check boxes have different names you would have to name all the fields in an array and then loop through the array. If the across field in the image above is anything but 1, the field names will have multiple decimals (.0.0, .0.1, .0.2, .1.0, etc.) making it difficult to run a loop. Luckily there’s a solution, and it can be used in both scenarios (1 decimal and multi-decimal field names).
Obtain an Array of Fields
The getArray( ) method returns an array of the all the field objects that were created from one root name. getArray( ).length returns the number of field objects in the array.
Practical Example
Create a check box called Check Box, then Right-click > Create multiple copies with 4 down and 4 across. You will end up with 16 fields called Check Box + decimals (Check Box.0.0 through Check Box.3.3). Run the following script in the console:
this.getField("Check Box").getArray();
It should return [object Field] 16 times, separated by commas. Now run the following script in the console:
this.getField("Check Box").getArray().length;
It should return 16.
Putting It All Together
The following script uses the getArray( ) method to count the number of questions answered:
var aray=this.getField("Check Box").getArray();
var num=0;
for(var i=0; i<aray.length; i++)
{
if(aray[i].value!="Off")
{num++}
}
// Now do something with num, which represents the number of boxes checked
The script above can be used all the time, without having to know the number of fields created from the root name.