Cognito-forms – Count the number of “Checked” Check Boxes

cognito-forms

I am using Cognito forms as part of an E-commerce Transaction. I have two issues that I do not seem to be able to overcome. They are as follows;

1: There are 20 checkboxes in a section called Practice Areas and the pricing is based on the following criteria;

1-5 Practice Areas = $200

6-10 Practice Areas = $300

etc..

So I need to be able to count the number of Check Boxes selected and then apply a calculation to it. for example, if the end-user selected 7 practice areas then the price would be $300

2: The 2nd issue is somewhat similar albeit a different workflow. The end-user will select from a drop down box either

1-5 Practice Areas ($200)

6-10 Practice Areas ($300)

Let's say the end-user selects the 6-10 Practice Areas ($300) option, then when the end-user is selecting the Practice Areas they want, by checking the appropriate checkbox, they are notified if they selected less than 6 or more than 10

Any help would be appreciated

Best Answer

I'm a developer with Cognito Forms.

This is something that can be set up with Cognito Forms. You will need a total of 3 fields working together, and a custom error message set up on the Choice field set to checkbox with the 20 checkbox items.

To get started add a Choice field labeled 'Number of Area Selection' to your form, set to the type of 'Drop down'. You can update all of the field labels after the calculations have been set up, this way ensures that all the calculations work right off the bat. Enable the 'Assign Values' option on this Choice field. I then used the following choice options with the values.

enter image description here

The next field is the Choice field with the 20 Checkbox items. I labeled this field 'Practice Areas' with the type of 'Checkboxes'. We will be adding a custom error to this field. The custom error is going to be checking our previous Choice field, and the current number of selected choice options. If the number is outside of the set range then the error message that you set up will be displayed. The following calculation can be entered into the 'Advanced Editor' the error message that is used is up to you, but an error message has to be added or the error will not display at all.

=(NumberOfAreaSelection_Value = 1 and PracticeAreas.Count() >= 6) or (NumberOfAreaSelection_Value = 2 and (PracticeAreas.Count() <=5 or PracticeAreas.Count() >= 11))or (NumberOfAreaSelection_Value = 3 and (PracticeAreas.Count() <=10 or PracticeAreas.Count() >= 16)) or (NumberOfAreaSelection_Value = 4 and PracticeAreas.Count() <= 15)

When running on your form the above two fields will work together to provide the following.

two choice fields working together to display an error

The last field that we will be setting up is a Price field with the label of 'Price'. This field will check the number of selected items in the Checkbox Choice field. While this calculation is not needed, it will show you how to check the count and provide the correct price based on the count.

=if PracticeAreas.Count() = 0 then 0 else if PracticeAreas.Count() <= 5 then 200 else if PracticeAreas.Count() <= 10 then 300 else if PracticeAreas.Count <= 15 then 400 else if PracticeAreas.Count <= 20 then 500 else 0

A number of Choice field checkboxes selected, and the price displaying the correct total.

If you would simply like to count the number of checked fields you can add a Calculation field to your form and target the Choice field, then add .Count(). To do this with the fields that I have set up you would need to use the following calculation.

=PracticeAreas.Count()

enter image description here

I have gone over a lot of working parts, that can be a bit hard to follow. I'm including a link to the form that I built with each field set up and working.

https://www.cognitoforms.com/templates/shared/DemoAccountForCognitoForms/CountTheNumberOfCheckedCheckBoxesDemo