> ## Documentation Index
> Fetch the complete documentation index at: https://docs.flowx.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Validators

> Apply pre-defined validation rules to form inputs such as text, number, email, and date fields across web, mobile, and desktop apps.

![](https://s3.eu-west-1.amazonaws.com/docx.flowx.ai/validators-add.png)

FlowX.AI provides default validators such as:

## Add a validator

1. Select the form element and find the **Validators** section in its **Settings** tab. An element with no validators shows **No validators yet**.
2. Click **Add new** (+). A **New validator** card appears and the **Add new validator** popover opens.
3. Choose a **Validator type**. The list shows only the validators that apply to the selected component, plus **Custom expression** and **Custom**.
4. Fill in the fields for that type. The value field is named after the validator (**Min value**, **Max value**, **Min length**, **Max length**, **Pattern**), or **Expression** for a custom expression. **Custom expression** and **Pattern** also have a **Label**, which becomes the name shown on the validator card.
5. Enter the **Error message** shown when the value is not valid.

Each saved validator appears as a card in the **Validators** section. Click a card to edit it, or use its delete button to remove it.

## Predefined validators

Each accordion title shows the label in the **Validator type** list, followed by the validator name saved in the configuration.

> [accordion] Min value (`min`)
>
> This validator checks whether a numeric value is smaller than the specified value. If there are no characters at all, this validator will not trigger. It is advisable to use this validator with a required validator.
>
> ![](https://s3.eu-west-1.amazonaws.com/docx.flowx.ai/validator-min.png)
>
> > [card] [min validator](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/min)
> >
> > Refer to MDN documentation for more details about the min attribute.

> [accordion] Max value (`max`)
>
> This validator checks whether a numeric value is larger than the specified value. If there are no characters at all, this validator will not trigger. It is advisable to use this validator with a [required](#predefined-validators) validator.
>
> ![](https://s3.eu-west-1.amazonaws.com/docx.flowx.ai/validator-max.png)
>
> > [card] [max validator](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/max)
> >
> > Refer to MDN documentation for more details about the max attribute.

> [accordion] Min length (`minlength`)
>
> This validator checks whether the input value has a minimum number of characters. If there are no characters at all, this validator will not trigger. It is advisable to use this validator with a required validator.
>
> ![](https://s3.eu-west-1.amazonaws.com/docx.flowx.ai/validator-minlength.png)
>
> > [card] [minlength](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/minlength)
> >
> > Refer to MDN documentation for more details about the minlength attribute.

> [accordion] Max length (`maxlength`)
>
> This validator checks whether the input value has a maximum number of characters. If there are no characters at all, this validator will not trigger. It is advisable to use this validator with a [required](#predefined-validators) validator.
>
> ![maxlength validator](https://s3.eu-west-1.amazonaws.com/docx.flowx.ai/validator-maxlength.png)
>
> > [card] [maxlength](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/maxlength)
> >
> > Refer to MDN documentation for more details about the maxlength attribute.

> [accordion] Required (`required`)
>
> This validator checks whether a value exists in the input field.
>
> ![Required validator on a form field](https://s3.eu-west-1.amazonaws.com/docx.flowx.ai/6.x/validator-required.png)
>
> It is recommended to use this validator with other validators like [minlength](#predefined-validators) to check if there is no value at all.
>
> ![Required validator error shown under an empty input at runtime](https://s3.eu-west-1.amazonaws.com/docx.flowx.ai/6.x/validator-required-runtime.png)
>
> > [card] [required](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/required)
> >
> > Refer to MDN documentation for more details about the required attribute.

> [accordion] Required true (`requiredTrue`)
>
> This validator checks that the value is `true`. It is the only predefined validator available for a [switch](https://docs.flowx.ai/6.x/docs/building-blocks/ui-designer/ui-component-types/form-elements/switch-form-field.md), where it makes the user turn the switch on before the form is valid (for example, to accept terms and conditions). It is also available for table cells.

> [accordion] Email (`email`)
>
> This validator checks whether the input value is a valid email. If there are no characters at all, this validator will not trigger. It is advisable to use this validator with a [required](#predefined-validators) validator.
>
> ![email validator](https://s3.eu-west-1.amazonaws.com/docx.flowx.ai/validator-email.png#center)
>
> > [card] [email](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email)
> >
> > Refer to MDN documentation for more details about email input validation.

> [accordion] Pattern (`pattern`)
>
> This validator checks whether the input value matches the specified pattern (for example, a [regex expression](https://www.regexbuddy.com/regex.html)).
>
> > [!WARNING]
> >
> > Pattern validators are meant for text input. If you need to validate numeric input with specific patterns (such as preventing leading zeros), either:
> >
> > * Use a text input field instead of a number input field, or
> > * Create a custom validator that handles the numeric validation logic
>
> ![](https://s3.eu-west-1.amazonaws.com/docx.flowx.ai/validator-pattern.png#center)
>
> > [card] [pattern validator](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern)
> >
> > Refer to MDN documentation for more details about the pattern attribute.

> [accordion] Custom expression (`customExpression`)
>
> This validator allows you to validate a form element based on computed expressions that can reference other values from the process data store, not just the current form element being validated.
>
> ![custom expression validator](https://s3.eu-west-1.amazonaws.com/docx.flowx.ai/validator-custom-expression.png)
>
> Example used:
>
> ```javascript Simple comparison theme={"dark"}
> return ${user.endDate} > ${user.startDate}
> ```
>
> The validator uses a code editor where you can write computed expressions that must eventually return `true` (valid) or `false` (invalid). The expression can contain process data store values and will be evaluated dynamically.
>
> > [!NOTE]
> >
> > The `customExpression` validator runs even when the field is empty. If the field is optional, return `true` for an empty value in the expression (see [Null and empty checks](#null-and-empty-checks)).
>
> **Key features:**
>
> * **Dynamic validation**: Based on other process values, not just the current field
> * **Computed expressions**: Write JavaScript expressions that return boolean values
> * **Real-time updates**: Re-evaluates when referenced process data changes
> * **Universal compatibility**: Available for all form elements
>
> **Expression examples:**
>
> ```javascript Simple comparison theme={"dark"}
> return ${app.endDate} > ${app.startDate}
> ```
>
> ```javascript Complex validation theme={"dark"}
> return ${app.userAge} >= 18 && ${app.hasParentConsent} === true
> ```
>
> ```javascript Numeric validation theme={"dark"}
> return ${app.totalAmount} <= ${app.availableBalance}
> ```
>
> > [!WARNING]
> >
> > **Important considerations:**
> >
> > * The expression **must** return a boolean value (`true` for valid, `false` for invalid)
> > * Use proper syntax when referencing process data store values with `${}`
> > * Make sure the referenced process data keys exist, so the expression does not evaluate against missing values
> > * The validator is asynchronous and will be re-evaluated when dependent data changes

Other predefined validators are also available:

> [accordion] Same or before today (`isSameOrBeforeToday`)
>
> This validator can be used to validate [datepicker](https://docs.flowx.ai/6.x/docs/building-blocks/ui-designer/ui-component-types/form-elements/datepicker-form-field.md) inputs. It checks whether the selected date is today or in the past. If there are no characters at all, this validator will not trigger. It is advisable to use this validator with a [required](#predefined-validators) validator.
>
> ![isSameOrBeforeToday](https://s3.eu-west-1.amazonaws.com/docx.flowx.ai/validator-same-or-before-today.png)

> [accordion] Same or after today (`isSameOrAfterToday`)
>
> This validator can be used to validate datepicker inputs. It checks whether the selected date is today or in the future. If there are no characters at all, this validator will not trigger. It is advisable to use this validator with a [required](#predefined-validators) validator.
>
> ![](https://s3.eu-west-1.amazonaws.com/docx.flowx.ai/validator-same-or-after-today.png)

## Validator compatibility with input field types

The Designer does not restrict validators by input type. The table shows which validators fit each kind of field:

| Validator | Text Fields | Number Fields | Email Fields | Date Fields |
| - | - | - | - | - |
| required | ✅ | ✅ | ✅ | ✅ |
| min | ❌ | ✅ | ❌ | ❌ |
| max | ❌ | ✅ | ❌ | ❌ |
| minlength | ✅ | ❌ | ✅ | ❌ |
| maxlength | ✅ | ❌ | ✅ | ❌ |
| email | ❌ | ❌ | ✅ | ❌ |
| pattern | ✅ | ❌ | ✅ | ❌ |
| customExpression | ✅ | ✅ | ✅ | ✅ |
| isSameOrBeforeToday | ❌ | ❌ | ❌ | ✅ |
| isSameOrAfterToday | ❌ | ❌ | ❌ | ✅ |

> [!NOTE]
>
> To ensure the validation of all form elements within a card upon executing a Save Data action such as "Submit" or "Continue," follow these steps:
>
> * When adding an event handler to a button inside a card, check **Add forms to submit** to show the forms dropdown. In a UI Flow, or in a collection prototype and its children, the option is labeled **Add forms to validate**.
>
> - From the dropdown menu, select the specific form or individual form elements that you wish to validate.
> - By choosing the appropriate form or elements from this dropdown, you can ensure comprehensive validation of your form.
>
> ![](https://s3.eu-west-1.amazonaws.com/docx.flowx.ai/button-event-handler-forms.png)

***

## Common expression patterns

The `customExpression` validator supports JavaScript expressions that return `true` (valid) or `false` (invalid). Below are patterns for common validation scenarios.

### Date comparison

Validate that one date comes after another:

```javascript theme={"dark"}
return new Date(${application.endDate}) > new Date(${application.startDate})
```

### Required-if (conditional required)

Make a field required only when another field has a specific value:

```javascript theme={"dark"}
if (${application.hasSpouse} === true) {
  return ${application.spouseName} !== null && ${application.spouseName} !== ""
}
return true
```

### Regex patterns

Validate a string against a pattern using `match()`:

```javascript UK National Insurance Number (NINO) theme={"dark"}
return /^[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-D]{1}$/.test(${application.nino})
```

```javascript Phone number (10 digits) theme={"dark"}
return /^\d{10}$/.test(${application.phoneNumber})
```

```javascript Postal code (UK format) theme={"dark"}
return /^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$/.test(${application.postalCode})
```

> [!TIP]
>
> For simple pattern matching on text fields, the [pattern validator](#predefined-validators) may be more appropriate. Use `customExpression` when you need to combine patterns with other logic.

### Null and empty checks

Handle optional fields that should be validated only when populated:

```javascript theme={"dark"}
if (${application.alternateEmail} === null || ${application.alternateEmail} === "") {
  return true
}
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(${application.alternateEmail})
```

### Cross-field validation

Compare multiple field values:

```javascript theme={"dark"}
return ${application.confirmPassword} === ${application.password}
```

> [!NOTE]
>
> **Hidden fields skip validation.** When a form element is hidden via a hide condition, all its validators (including `customExpression`) are disabled. Hidden fields do not block form submission. If you need to validate data regardless of field visibility, use a [business rule action](https://docs.flowx.ai/6.x/docs/building-blocks/actions/business-rule-action/business-rule-action.md) on the backend.
>
> For full details on how visibility affects validation, see [Event Handlers — Form validation](https://docs.flowx.ai/6.x/docs/building-blocks/ui-designer/event-handlers.md#form-validation).

***

## Custom validators

Additionally, custom validators can be created within the web application and referenced by name. These custom validators can have various configurations such as execution type, name, parameters, and error message.

To add one, set **Validator type** to **Custom**, then fill in:

1. **Execution type** - synchronous/asynchronous validator
2. **Name** - name provided by the developer to uniquely identify the validator
3. **Params** - if the validator needs inputs to decide if the field is valid or not, you can pass them using this list
4. **Error message** - the message that will be displayed if the field is not valid

> [!WARNING]
>
> In the Angular renderer, the error key that the validator returns **MUST** match the validator name. In the React renderer, a custom validator returns `true` (valid) or `false` (invalid).

![custom validator](https://s3.eu-west-1.amazonaws.com/docx.flowx.ai/validator-custom.png#center)

### Custom validator example

Below you can find an example of a custom validator (`currentOrLastYear`) that restricts date selection to the current or the previous year:

#### currentOrLastYear

```typescript theme={"dark"}
currentOrLastYear: function currentOrLastYear(AC: AbstractControl): { [key: string]: any } {
    if (!AC) {
      return null;
    }

    const yearDate = moment(AC.value, YEAR_FORMAT, true);
    const currentDateYear = moment(new Date()).startOf('year');
    const lastYear = moment(new Date()).subtract(1, 'year').startOf('year');

    if (!yearDate.isSame(currentDateYear) && !yearDate.isSame(lastYear)) {
      return { currentOrLastYear: true };
    }

    return null;
}
```

#### smallerOrEqualsToNumber

Below is another custom validator example that validates form input asynchronously. The validator is called `smallerOrEqualsToNumber` and takes an array of `params` as an input.

> [!NOTE]
>
> For this custom validator the execution type should be marked as `async` using the UI Designer.

```typescript theme={"dark"}
export function smallerOrEqualsToNumber (params$: Observable<any>[]): AsyncValidatorFn {
  return (AC): Promise<ValidationErrors | null> | Observable<ValidationErrors | null> => {
    return new Observable((observer) => {
      combineLatest(params$).subscribe(([maximumLoanAmount]) => {
        const validationError =
          maximumLoanAmount === undefined || !AC.value || Number(AC.value) <= maximumLoanAmount ? null : {smallerOrEqualsToNumber: true};

        observer.next(validationError);
        observer.complete();
      });
    });
  };
}
```

If the input value is undefined or the input value is smaller or equal to the maximum loan amount value, the function returns `null`, indicating that the input is valid. If the input value is greater than the maximum loan amount value, the function returns a validation error object with a key `smallerOrEqualsToNumber` and a value of true, indicating that the input is invalid.

> [!NOTE]
>
> For more details about custom validators, see **Custom validators** in the [Angular renderer](https://docs.flowx.ai/6.x/sdks/angular-renderer.md#custom-validators) and [React renderer](https://docs.flowx.ai/6.x/sdks/react-renderer.md#custom-validators) SDK pages.

> [!TIP]
>
> Using validators in your application can help ensure that the data entered by users is valid, accurate, and consistent, improving the overall quality of your application.

It can also help prevent errors and bugs that may arise due to invalid data, saving time and effort in debugging and fixing issues.

***

## Academy courses

> [card] [Hide and Disable course](https://academy.flowx.ai/explore/hide-and-disable-ui-elements)
>
> Academy course on building dynamic UI logic with hide and disable conditions, including validation behavior


## Related topics

- [Multi select](https://docs.flowx.ai/6.x/docs/building-blocks/ui-designer/ui-component-types/form-elements/multi-select.md)
- [Checkbox](https://docs.flowx.ai/6.x/docs/building-blocks/ui-designer/ui-component-types/form-elements/checkbox-form-field.md)
- [Radio](https://docs.flowx.ai/6.x/docs/building-blocks/ui-designer/ui-component-types/form-elements/radio-form-field.md)
- [Slider](https://docs.flowx.ai/6.x/docs/building-blocks/ui-designer/ui-component-types/form-elements/slider.md)
- [Segmented button](https://docs.flowx.ai/6.x/docs/building-blocks/ui-designer/ui-component-types/form-elements/segmented-button.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.