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

参见

说明

验证器就是一个函数,它可以处理单个 FormControl 好一组控件,并返回一个错误映射表(map)或 null。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.

静态方法

此验证器要求控件的值大于或等于指定的数字。 它只有函数形式,没有指令形式。

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
      
      static min(min: number): ValidatorFn
    
参数
min number
返回值

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

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

使用说明

验证至少为 3
Validate against a minimum of 3
const control = new FormControl(2, Validators.min(3)); console.log(control.errors); // {min: {min: 3, actual: 2}}
      
      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
      
      static max(max: number): ValidatorFn
    
参数
max number
返回值

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

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

使用说明

验证最大为 15
Validate against a maximum of 15
const control = new FormControl(16, Validators.max(15)); console.log(control.errors); // {max: {max: 15, actual: 16}}
      
      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
      
      static required(control: AbstractControl): ValidationErrors | null
    
参数
control AbstractControl
返回值

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

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

使用说明

验证该字段不是空的
Validate that the field is non-empty
const control = new FormControl('', Validators.required); console.log(control.errors); // {required: true}
      
      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
      
      static requiredTrue(control: AbstractControl): ValidationErrors | null
    
参数
control AbstractControl
返回值

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

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

使用说明

验证字段值为真
Validate that the field value is true
const control = new FormControl('', Validators.requiredTrue); console.log(control.errors); // {required: true}
      
      const control = new FormControl('', Validators.requiredTrue);

console.log(control.errors); // {required: true}
    

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

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

static email(control: AbstractControl): ValidationErrors | null
      
      static email(control: AbstractControl): ValidationErrors | null
    
参数
control AbstractControl
返回值

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

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

使用说明

验证该字段匹配有效的 email 格式。
Validate that the field matches a valid email pattern
const control = new FormControl('bad@', Validators.email); console.log(control.errors); // {email: true}
      
      const control = new FormControl('bad@', Validators.email);

console.log(control.errors); // {email: true}
    

此验证器要求控件值的长度大于等于所指定的最小长度。当使用 HTML5 的 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): ValidatorFn
      
      static minLength(minLength: number): ValidatorFn
    
参数
minLength number
返回值

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

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

使用说明

验证该字段至少有 3 个字符
Validate that the field has a minimum of 3 characters
const control = new FormControl('ng', Validators.minLength(3)); console.log(control.errors); // {minlength: {requiredLength: 3, actualLength: 2}}
      
      const control = new FormControl('ng', Validators.minLength(3));

console.log(control.errors); // {minlength: {requiredLength: 3, actualLength: 2}}
    
<input minlength="5">
      
      <input minlength="5">
    

此验证器要求控件值的长度小于等于所指定的最大长度。当使用 HTML5 的 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): ValidatorFn
      
      static maxLength(maxLength: number): ValidatorFn
    
参数
maxLength number
返回值

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

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

使用说明

验证该字段最多具有 5 个字符
Validate that the field has maximum of 5 characters
const control = new FormControl('Angular', Validators.maxLength(5)); console.log(control.errors); // {maxlength: {requiredLength: 5, actualLength: 7}}
      
      const control = new FormControl('Angular', Validators.maxLength(5));

console.log(control.errors); // {maxlength: {requiredLength: 5, actualLength: 7}}
    
<input maxlength="5">
      
      <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.

static pattern(pattern: string | RegExp): ValidatorFn
      
      static pattern(pattern: string | RegExp): ValidatorFn
    
参数
pattern string | RegExp
返回值

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

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

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

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.

使用说明

验证该字段只包含字母或空格
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 ]*$', actualValue: '1'}}
      
      const control = new FormControl('1', Validators.pattern('[a-zA-Z ]*'));

console.log(control.errors); // {pattern: {requiredPattern: '^[a-zA-Z ]*$', actualValue: '1'}}
    
<input pattern="[a-zA-Z ]*">
      
      <input pattern="[a-zA-Z ]*">
    

此验证器什么也不做。

Validator that performs no operation.

static nullValidator(control: AbstractControl): ValidationErrors | null
      
      static nullValidator(control: AbstractControl): ValidationErrors | null
    
参数
control AbstractControl
返回值

ValidationErrors | null

把多个验证器合并成一个函数,它会返回指定控件的各个错误映射表的并集。

Compose multiple validators into a single function that returns the union of the individual error maps for the provided control.

static compose(validators: null): null
      
      static compose(validators: null): null
    
参数
validators null
返回值

如果验证失败,则此验证器函数返回各个验证器所返回错误对象的一个并集,否则为 null

null: A validator function that returns an error map with the merged error maps of the validators if the validation check fails, otherwise null.

static compose(validators: ValidatorFn[]): ValidatorFn | null
      
      static compose(validators: ValidatorFn[]): ValidatorFn | null
    
参数
validators ValidatorFn[]
返回值

ValidatorFn | null

把多个异步验证器合并成一个函数,它会返回指定控件的各个错误映射表的并集。

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 | null
      
      static composeAsync(validators: AsyncValidatorFn[]): AsyncValidatorFn | null
    
参数
validators AsyncValidatorFn[]
返回值

如果验证失败,则此验证器函数返回各异步验证器所返回错误对象的一个并集,否则为 null

AsyncValidatorFn | null: A validator function that returns an error map with the merged error objects of the async validators if the validation check fails, otherwise null.