Custom Form Validation Using Expressions

Edit in →

Custom JavaScript validation for forms allows you to create more specific input validation logic than built-in validators. SurveyJS Form Library supports two ways to add custom validation—with an event or expressions. This example demonstrates how to configure expressions. Switch between React, Vue, Knockout, jQuery, and Angular to view the example for your JavaScript framework.

Event Handler vs Expressions

Event Handler

To create custom form validation in JavaScript with the onValidateQuestion event, you need to implement a corresponding event handler. Users have no access to this handler and therefore cannot disable custom validation. Refer to the following demo for more information: Custom Form Validation Using an Event.

Expressions

For custom validation with expressions, you need to implement and register a custom validator in your JavaScript code to make the validator available within expressions. Users cannot modify the validator, but they can decide whether or not to use it in their expressions when they design their surveys and thus enable or disable custom validation.

Implement a Custom Validator

Follow the steps below to implement a custom validator:

  1. Create a JavaScript function that validates data.
    This function should return a Boolean value that indicates a validation result (true if the value is valid, false otherwise).

  2. Register the function in FunctionFactory.
    The following code registers the myFunc function under the name foo:

    import { FunctionFactory } from "survey-core";
    FunctionFactory.Instance.register("foo", myFunc);
    
  3. Use the validator within expressions.
    To reference the validator within an expression, use curly brackets: {foo}.

In this custom form validation example, the form field accepts only e-mail addresses hosted at surveyjs.io. The isDomainValid custom validator checks whether the entered e-mail address is valid. If the validation fails, the form field displays an error message. Validation is triggered immediately after you leave the input field because the Survey's checkErrorsMode property is set to "onValueChanged". If you do not set this property, validation activates before a user proceeds to the next page or completes the survey.

Your cookie settings

We use cookies on our site to make your browsing experience more convenient and personal. In some cases, they are essential to making the site work properly. By clicking "Accept All", you consent to the use of all cookies in accordance with our Terms of Use & Privacy Statement. However, you may visit "Cookie settings" to provide a controlled consent.

Your renewal subscription expires soon.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.

Your renewal subscription has expired.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.