Is there a way to create a validation with regex pattern in Sitecore Form to check whether a checkbox is checked or not?
If you want to make the checkbox required, then on the control you can check it as required.
But if there is something else that you want to do using Regex patterns, then you can create your custom regex validator.
To do this you need to do the following.
- In the Content Editor, go to
- Right-click Validations, click Insert, Insert From Template
- In the Insert from Template dialog box, go to
/System/Formsand click the Validation template
- With the item you just created selected in the content tree, click the Content tab and, in the Settings section, in the Type field, set the value to the existing regular expression validation class type name (for example,
- In the Message field, enter an error message, for example, Contains an invalid name.
- In the Parameters field, set the regular expression pattern to validate
For the checkbox, you can try adding this pattern.
Follow to this Sitecore document for more details.
You can create a validation with a regex pattern in Sitecore Forms to check whether a checkbox is checked or not. Here are the steps:
Open the Sitecore Forms Editor and select the checkbox field that you want to validate.
In the Validation section of the field settings, click the Add button to create a new validation.
In the Validation Type drop-down, select Regular Expression.
In the Regular Expression field, enter the following pattern:
^on$. This pattern matches the string "on", which is the value submitted by a checkbox when it is checked.
In the Error Message field, enter the error message that should be displayed if the validation fails, such as "Please check the box to continue".
Save the field settings and publish the form.
With this validation in place, the form will only be submitted if the checkbox is checked. If the checkbox is not checked, the error message will be displayed to the user.
As your question is related to required field validation only and Regex does not work OOTB with the required field. You might notice that the most used validation - required field - is not in the list. That is because this is a special case.. also on a Forms field, you would not select a required validator but just tick the required checkbox.
You can explore 2 more options are:
- Custom validation: Sitecore Forms Custom Validation
- Custom checkbox element: Sitecore Forms Custom Checkbox Element
Hope it helps you to move forward in a possible ways.