Validate field inputs using Custom Validators

Custom Validators feature is used to define validation rules based on regular expressions while adding a product to the cart. This validation is assigned to any particular field which will be validated before adding the product to the cart. To create a new custom validator,

  1. Go to Extra Product Options >> Advanced Settings tab. You can see a section Custom Validators with 4 fields showing Validator Name, Validator Label, Validator Pattern, and Validator Message.
  2. Fill the fields according to your requirements. Enter the regular expression to be validated in the Validator Pattern field. (Refer the screenshot.)
  3. To add more than one validator, click on the ‘Add New Validator’ (+) button to get a new row where you can add further.

    Following are the input values for Validator Fields:

    • Validator Name: Provide a unique name (combination of letters and numbers without space) for the validation rule, which will be used internally to identify the validator.
    • Validator Label: The name provided here will be the display name for the validation rule.
    • Validator Pattern: In this field, add a regular expression to match a valid input. The regular expression must start with /^ and end with $/. Enter the regular expression between them.

      Ex: A regular expression for collecting a combination of 3 small letters, 3 capital letters, and 3 numbers is as given below:

      /^[a-z]{3}[A-Z]{3}[0-9]{3}$/

    • Validation Message: Provide the message to be displayed when the validation fails. Use %s: to include field name in the validation messages.
  4. Save the modifications clicking on ‘Save the changes’ button.

    Once the validator is successfully created and updated, you can go to the Product Options tab and edit/add a product field. Here, you can choose the validator from the Validations pulldown field.