0

Is there a way to create a validation with regex pattern in Sitecore Form to check whether a checkbox is checked or not?

2
  • Did you try by making this as required? Mar 29, 2023 at 7:45
  • yes but that will show one default required message. I need custom message only for this control not for all.
    – Prateek
    Mar 29, 2023 at 7:52

3 Answers 3

1

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.

  1. In the Content Editor, go to /sitecore/System/Settings/Forms/Validations
  2. Right-click Validations, click Insert, Insert From Template
  3. In the Insert from Template dialog box, go to /System/Forms and click the Validation template
  4. 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, Sitecore.ExperienceForms.Mvc.Models.Validation.RegularExpressionValidation,Sitecore.ExperienceForms.Mvc).
  5. In the Message field, enter an error message, for example, Contains an invalid name.
  6. In the Parameters field, set the regular expression pattern to validate

For the checkbox, you can try adding this pattern.

{"regularExpression":"^/true/"}

Follow to this Sitecore document for more details.

https://doc.sitecore.com/xp/en/developers/91/sitecore-experience-manager/walkthrough--creating-a-custom-validator-based-on-the-existing-regular-expression-validation.html

0

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:

  1. Open the Sitecore Forms Editor and select the checkbox field that you want to validate.

  2. In the Validation section of the field settings, click the Add button to create a new validation.

  3. In the Validation Type drop-down, select Regular Expression.

  4. 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.

  5. 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".

  6. 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.

1
  • I am not seeing any add button to create validation for filed from forms editor.
    – Prateek
    Mar 29, 2023 at 11:31
0

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:

  1. Custom validation: Sitecore Forms Custom Validation
  2. Custom checkbox element: Sitecore Forms Custom Checkbox Element

Hope it helps you to move forward in a possible ways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.