Validators

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(c: AbstractControl): ValidationErrors | null static compose(validators: (ValidatorFn | null | undefined)[] | null): ValidatorFn | null static composeAsync(validators: (AsyncValidatorFn | null)[]): AsyncValidatorFn | null }

参见

说明

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.

验证器就是一个函数,它可以处理单个 FormControl 好一组控件,并返回一个错误映射表(map)或 null。null 表示验证已通过了。

静态方法

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): ValidatorFn

参数

min

Type: number.

返回值

ValidatorFn: A validator function that returns an error map with the min property if the validation check fails, otherwise null.

如果验证失败,则此验证器函数返回一个带有 min 属性的映射表(map),否则为 null

Validate against a minimum of 3

验证至少为 3

const control = new FormControl(2, Validators.min(3)); console.log(control.errors); // {min: {min: 3, actual: 2}}

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): ValidatorFn

参数

max

Type: number.

返回值

ValidatorFn: A validator function that returns an error map with the max property if the validation check fails, otherwise null.

如果验证失败,则此验证器函数返回一个带有 max 属性的映射表(map),否则为 null

Validate against a maximum of 15

验证最大为 15

const control = new FormControl(16, Validators.max(15)); console.log(control.errors); // {max: {max: 15, actual: 16}}

Validator that requires the control have a non-empty value.

此验证器要求控件具有非空值。

static required(control: AbstractControl): ValidationErrors | null

参数

control

Type: AbstractControl.

返回值

ValidationErrors | null: An error map with the required property if the validation check fails, otherwise null.

如果验证失败,则此验证器函数返回一个带有 required 属性的映射表(map),否则为 null

Validate that the field is non-empty

验证该字段不是空的

const control = new FormControl('', Validators.required); console.log(control.errors); // {required: true}

Validator that requires the control's value be true. This validator is commonly used for required checkboxes.

此验证器要求控件的值为真。它通常用来验证检查框。

static requiredTrue(control: AbstractControl): ValidationErrors | null

参数

control

Type: AbstractControl.

返回值

ValidationErrors | null: An error map that contains the required property set to true if the validation check fails, otherwise null.

如果验证失败,则此验证器函数返回一个带有 required 属性、值为 true 的映射表(map),否则为 null

Validate that the field value is true

验证字段值为真

const control = new FormControl('', Validators.requiredTrue); console.log(control.errors); // {required: true}

Validator that requires the control's value pass an email validation test.

此验证器要求控件的值能通过 email 格式验证。

static email(control: AbstractControl): ValidationErrors | null

参数

control

Type: AbstractControl.

返回值

ValidationErrors | null: An error map with the email property if the validation check fails, otherwise null.

如果验证失败,则此验证器函数返回一个带有 email 属性的映射表(map),否则为 null

Validate that the field matches a valid email pattern

验证该字段匹配有效的 email 格式。

const control = new FormControl('bad@', Validators.email); console.log(control.errors); // {email: true}

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.

此验证器要求控件值的长度大于等于所指定的最小长度。当使用 HTML5 的 minlength 属性时,此验证器也会生效。

static minLength(minLength: number): ValidatorFn

参数

minLength

Type: number.

返回值

ValidatorFn: A validator function that returns an error map with the minlength if the validation check fails, otherwise null.

如果验证失败,则此验证器函数返回一个带有 minlength 属性的映射表(map),否则为 null

Validate that the field has a minimum of 3 characters

验证该字段至少有 3 个字符

const control = new FormControl('ng', Validators.minLength(3)); console.log(control.errors); // {minlength: {requiredLength: 3, actualLength: 2}}<input minlength="5">

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.

此验证器要求控件值的长度小于等于所指定的最大长度。当使用 HTML5 的 maxlength 属性时,此验证器也会生效。

static maxLength(maxLength: number): ValidatorFn

参数

maxLength

Type: number.

返回值

ValidatorFn: A validator function that returns an error map with the maxlength property if the validation check fails, otherwise null.

如果验证失败,则此验证器函数返回一个带有 maxlength 属性的映射表(map),否则为 null

Validate that the field has maximum of 5 characters

验证该字段最多具有 5 个字符

const control = new FormControl('Angular', Validators.maxLength(5)); console.log(control.errors); // {maxlength: {requiredLength: 5, actualLength: 7}}<input maxlength="5">

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.

此验证器要求控件的值匹配某个正则表达式。当使用 HTML5 的 pattern 属性时,它也会生效。

static pattern(pattern: string | RegExp): ValidatorFn

参数

pattern

Type: string | RegExp.

返回值

ValidatorFn: A validator function that returns an error map with the pattern property if the validation check fails, otherwise null.

如果验证失败,则此验证器函数返回一个带有 pattern 属性的映射表(map),否则为 null

Validate that the field only contains letters or spaces

验证该字段只包含字母或空格

const control = new FormControl('1', Validators.pattern('[a-zA-Z ]*')); console.log(control.errors); // {pattern: {requiredPattern: '^[a-zA-Z ]*