Validators class provides a set of built-in validators that can be used by form controls. A validator is a function that processes a FormControl or collection of controls and returns an error map or null. A null map means that validation has passed.
Import
Built-in Validators
required
Validator that requires the control have a non-empty value.ValidationErrors | null
An error map with the
required property if the validation check fails, otherwise null.requiredTrue
Validator that requires the control’s value be true. This validator is commonly used for required checkboxes.ValidationErrors | null
An error map with the
required property set to true if the validation check fails, otherwise null.- Disallows
local-partto begin or end with a period (.) - Disallows
local-partto be longer than 64 characters - Disallows the whole address to be longer than 254 characters
ValidationErrors | null
An error map with the
email property if the validation check fails, otherwise null.min()
Validator that requires the control’s value to be greater than or equal to the provided number.number
required
The minimum value.
ValidatorFn
A validator function that returns an error map with the
min property if the validation check fails, otherwise null.max()
Validator that requires the control’s value to be less than or equal to the provided number.number
required
The maximum value.
ValidatorFn
A validator function that returns an error map with the
max property if the validation check fails, otherwise null.minLength()
Validator that requires the number of items in the control’s value to be greater than or equal to the provided minimum length. This validator is intended for types that have a numericlength or size property, such as strings, arrays, or sets. The validator logic is not invoked for values when their length or size property is 0.
number
required
The minimum length.
ValidatorFn
A validator function that returns an error map with the
minlength property if the validation check fails, otherwise null.maxLength()
Validator that requires the number of items in the control’s value to be less than or equal to the provided maximum length. This validator is intended for types that have a numericlength or size property, such as strings, arrays, or sets.
number
required
The maximum length.
ValidatorFn
A validator function that returns an error map with the
maxlength property if the validation check fails, otherwise null.pattern()
Validator that requires the control’s value to match a regex pattern.string | RegExp
required
A regular expression to test the values, or a string. If a string is passed, the
^ character is prepended and the $ character is appended (if not already present).ValidatorFn
A validator function that returns an error map with the
pattern property if the validation check fails, otherwise null.nullValidator()
Validator that performs no operation.ValidationErrors | null
Always returns
null.Composing Validators
compose()
Composes multiple validators into a single function that returns the union of the individual error maps.(ValidatorFn | null | undefined)[]
required
An array of validator functions to compose.
ValidatorFn | null
A validator function that returns an error map with the merged error maps of the validators if the validation check fails, otherwise
null.composeAsync()
Composes multiple async validators into a single function that returns the union of the individual error objects.(AsyncValidatorFn | null)[]
required
An array of async validator functions to compose.
AsyncValidatorFn | null
A validator function that returns an error map with the merged error objects if the validation check fails, otherwise
null.Using Multiple Validators
You can apply multiple validators to a single control:Custom Validators
Creating a Custom Validator
Custom Async Validator
Cross-Field Validation
Validation Errors
Each validator returns aValidationErrors object when validation fails:
Checking for Specific Errors
Dynamic Validators
You can add or remove validators dynamically:Validator Directives
You can create custom validator directives for template-driven forms:Validation Timing
Control when validation occurs usingupdateOn:
Injection Tokens
NG_VALIDATORS
Injection token for registering additional synchronous validators.NG_ASYNC_VALIDATORS
Injection token for registering additional asynchronous validators.See Also
- FormControl - Apply validators to individual controls
- FormGroup - Group-level validation
- Form Validation Guide
- Custom Validators Guide
