Validators
class
Provides a set of built-in validators that can be used by form controls.
class Validators {
static min(min: number): ValidatorFn
static max(max: number): ValidatorFn
static required(control: AbstractControl): ValidationErrors | null
static requiredTrue(control: AbstractControl): ValidationErrors | null
static email(control: AbstractControl): ValidationErrors | null
static minLength(minLength: number): ValidatorFn
static maxLength(maxLength: number): ValidatorFn
static pattern(pattern: string | RegExp): ValidatorFn
static nullValidator(control: AbstractControl): ValidationErrors | null
static compose(validators: ValidatorFn[]): ValidatorFn | null
static composeAsync(validators: AsyncValidatorFn[]): AsyncValidatorFn | null
}
See also
Description
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.
Static methods
min() | |||
---|---|---|---|
Validator that requires the control's value to be greater than or equal to the provided number. The validator exists only as a function and not as a directive. | |||
static min(min: number): ValidatorFnParameters
Returns
| |||
Usage NotesValidate against a minimum of 3
|
max() | |||
---|---|---|---|
Validator that requires the control's value to be less than or equal to the provided number. The validator exists only as a function and not as a directive. | |||
static max(max: number): ValidatorFnParameters
Returns
| |||
Usage NotesValidate against a maximum of 15
|
required() | |||
---|---|---|---|
Validator that requires the control have a non-empty value. | |||
static required(control: AbstractControl): ValidationErrors | nullParameters
Returns
| |||
Usage NotesValidate that the field is non-empty
|
requiredTrue() | |||
---|---|---|---|
Validator that requires the control's value be true. This validator is commonly used for required checkboxes. | |||
static requiredTrue(control: AbstractControl): ValidationErrors | nullParameters
Returns
| |||
Usage NotesValidate that the field value is true
|
email() | |||
---|---|---|---|
Validator that requires the control's value pass an email validation test. | |||
static email(control: AbstractControl): ValidationErrors | nullParameters
Returns
| |||
Usage NotesValidate that the field matches a valid email pattern
|
minLength() | |||
---|---|---|---|
Validator that requires the length of the control's value to be greater than or equal to the provided minimum length. This validator is also provided by default if you use the the HTML5 minlength attribute.
| |||
static minLength(minLength: number): ValidatorFnParameters
Returns
| |||
Usage NotesValidate that the field has a minimum of 3 characters
|
maxLength() | |||
---|---|---|---|
Validator that requires the length of the control's value to be less than or equal to the provided maximum length. This validator is also provided by default if you use the the HTML5 maxlength attribute.
| |||
static maxLength(maxLength: number): ValidatorFnParameters
Returns
| |||
Usage NotesValidate that the field has maximum of 5 characters
|
pattern() | |||
---|---|---|---|
Validator that requires the control's value to match a regex pattern. This validator is also provided by default if you use the HTML5 pattern attribute.
| |||
static pattern(pattern: string | RegExp): ValidatorFnParameters
Returns
| |||
Note that if a Regexp is provided, the Regexp is used as is to test the values. On the other hand, if a string is passed, the ^ character is prepended and the $ character is appended to the provided string (if not already present), and the resulting regular expression is used to test the values.
| |||
Usage NotesValidate that the field only contains letters or spaces
|
nullValidator() | |||
---|---|---|---|
Validator that performs no operation. | |||
static nullValidator(control: AbstractControl): ValidationErrors | nullParameters
Returns
|
compose() | |||
---|---|---|---|
Compose multiple validators into a single function that returns the union of the individual error maps for the provided control.
static compose(validators: null): nullParameters
Returns
| |||
static compose(validators: ValidatorFn[]): ValidatorFn | nullParameters
Returns
|
composeAsync() | |||
---|---|---|---|
Compose multiple async validators into a single function that returns the union of the individual error objects for the provided control. | |||
static composeAsync(validators: AsyncValidatorFn[]): AsyncValidatorFn | nullParameters
Returns
|
© 2010–2019 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v7.angular.io/api/forms/Validators