Calculations involving Checkboxes
Using Checkboxes in Calculations & Visibility Rules
Overview
Checkbox fields allow users to select one or more options from a list. When multiple options are selected, pro-Forms stores the result as a single concatenated string where each chosen option is separated by a comma. For example, if a user selects Monday, Wednesday, and Friday, the stored value is:
Monday,Wednesday,Friday
This behaviour is important to understand when writing calculations or visibility rules that reference a checkbox field.
The Problem: Using EXACT with Checkboxes
A common mistake is using EXACT() (or the = operator) to check whether a specific option has been selected. This works correctly when only one option is chosen, but fails as soon as the user picks more than one option.
|
⚠ Why this breaks When the user selects Monday only, the field value is Monday — so EXACT(#checkbox_field#, "Monday") returns true. But when the user selects Monday and Wednesday, the field value becomes Monday,Wednesday. Now EXACT(#checkbox_field#, "Monday") compares "Monday,Wednesday" with "Monday" — which returns false, even though Monday is selected. |
Incorrect Example
The following visibility rule is intended to show a follow-up question when the user selects Monday:
EXACT(#availability#, "Monday")
This will fail if the user also selects any other option alongside Monday.
The Solution: Use CONTAINS()
The correct approach is to use the CONTAINS() function, which checks whether a value appears anywhere within a string. Because the checkbox field stores all selected options as a comma-separated string, CONTAINS() will correctly identify a specific option regardless of how many others are also selected.
The syntax is:
CONTAINS("option to find", #checkbox_field#)
Note that the option to search for comes first, and the field to search within comes second. This mirrors the CONTAINS function documented in the Calculations reference.
Correct Example
To show a follow-up question when the user has selected Monday:
CONTAINS("Monday", #availability#)
This will correctly return true whether the field value is Monday, Monday,Wednesday, or Monday,Wednesday,Friday.
EXACT vs CONTAINS — at a Glance
|
Selected Options |
Field value stored |
EXACT(#field#, "Monday") |
CONTAINS("Monday", #field#) |
|
Monday only |
Monday |
✔ true |
✔ true |
|
Monday + Wednesday |
Monday,Wednesday |
✘ false |
✔ true |
|
Wednesday + Friday |
Wednesday,Friday |
✘ false |
✔ false |
|
Wednesday only |
Wednesday |
✘ false |
✔ false |
Checking Multiple Checkbox Options
Show a field when ANY of several options is selected — OR()
Wrap multiple CONTAINS() calls inside OR() to show a field when the user has selected at least one of a set of options:
OR( CONTAINS("Monday", #availability#), CONTAINS("Tuesday", #availability#) )
This returns true if Monday or Tuesday (or both) appear in the field.
Show a field only when ALL specified options are selected — AND()
Use AND() when a follow-up question should only appear if the user has selected a combination of options:
AND( CONTAINS("Monday", #availability#), CONTAINS("Wednesday", #availability#) )
This returns true only if both Monday and Wednesday are among the selected values.
Hide a field when a specific option is selected — NOT()
Reverse the result with NOT() to hide a field when a particular option is present:
NOT( CONTAINS("N/A", #availability#) )
This hides the field when the user has selected N/A.
Counting Selected Options with COUNT()
The COUNT() function returns the number of options currently selected in a checkbox (or radio/dropdown) field. This is useful for validation, scoring, or conditionally showing summary information.
Basic usage
COUNT(#availability#)
Returns the number of options the user has ticked. For example, if Monday,Wednesday,Friday is selected, this returns 3.
Show a warning when too many options are selected
Add a visibility rule to a warning label field:
COUNT(#availability#) > 3
The warning field can become visible only if more than three options are chosen.
Require at least one selection
To show a 'please select at least one option' message:
COUNT(#availability#) < 1
Display the count in another field
Set the Calculation of a read-only text or number field to:
COUNT(#availability#)
The field will show the live count as the user ticks and unticks boxes.
Using IF() with Checkboxes
You can combine CONTAINS() or COUNT() with IF() to compute dynamic values based on what the user has selected.
Set a field value based on a selected option
This example sets a follow-up field to a specific message depending on which service the user has requested:
IF( CONTAINS("Emergency", #service_type#), "A supervisor will be contacted immediately", "Standard processing applies" )
Calculate a cost based on options chosen
Each selected option adds to a running total using IF() statements:
ADD(
IF( CONTAINS("Site Visit", #services#), 150, 0 ),
IF( CONTAINS("Report", #services#), 75, 0 ),
IF( CONTAINS("Consultation", #services#), 50, 0 )
)
Each IF() adds the service price if that option is selected, or 0 if it is not. The ADD() sums the results into a total cost.
Score a checklist
If each checkbox option represents a completed task worth one point, COUNT() effectively gives the score:
MULTIPLY( COUNT(#completed_items#), 10 )
Multiplies the number of selected items by 10 to give a percentage-style score when there are 10 options.
Quick Reference
The table below summarises the most common patterns when working with checkbox fields.
|
Calculation |
What it does |
|
CONTAINS("Option", #field#) |
Returns true if Option is among the selected values |
|
NOT( CONTAINS("Option", #field#) ) |
Returns true if Option is NOT selected |
|
OR( CONTAINS("A",#field#), CONTAINS("B",#field#) ) |
Returns true if A or B (or both) are selected |
|
AND( CONTAINS("A",#field#), CONTAINS("B",#field#) ) |
Returns true only if both A and B are selected |
|
COUNT(#field#) |
Returns the number of options selected |
|
COUNT(#field#) > 2 |
Returns true if more than 2 options are selected |
|
COUNT(#field#) < 1 |
Returns true if no options are selected (nothing ticked) |
|
IF( CONTAINS("X",#field#), "Yes", "No" ) |
Returns Yes or No based on whether X is selected |