Team Forms offers powerful validation features to ensure that the information entered is well-formatted before submission. This helps prevent incorrect data entries and eliminates the need for back-and-forth communication requesting users to correct issues. One useful validation feature allows you to restrict the number of selections allowed in a component. However, how you apply this validation depends on the specific component. This article covers some common examples.
File Component
When the File component is configured to allow multiple files, you may still want to limit the total number of files a user can upload. To enforce this restriction:
Navigate to the **Validation** tab of the File component.
Under **Custom Validation**, include the following JavaScript check. Since the File component stores uploaded files in an array, you can use the array's length to validate the response:
valid = instance.getValue().length < 5;
This validation ensures that users cannot upload more than four files.
Drop-Down Menu
When a Drop-down menu (or even a SharePoint Drop-down) is configured to allow multiple selections, users can select any number of items from the list. However, you may want to limit the total number of selections. To do this:
Navigate to the **Validation** tab of the Drop-down menu or SharePoint data component.
Under **Custom Validation**, enter the following JavaScript check. When the "Allow Multiple" option is enabled, Team Forms stores each selection in an array, allowing us to validate the array length:
valid = instance.getValue().length < 5;
This ensures that users cannot select more than four options.
Select Box
A Select Box displays multiple checkboxes, allowing users to select multiple options. To restrict the number of selections:
Navigate to the Component Settings for the Select Box.
Go to the Validation tab.
Set the Maximum checked number and Minimum checked number options to define the allowed range of selections.
This method provides an easy way to control the number of selections without requiring custom validation scripts.