PatternValidator

该指令会借助 NG_VALIDATORS 绑定来把 pattern 验证器添加到任何带有 pattern 属性的控件上。 它会使用该属性的值作为正则表达式来验证控件的值。 它会遵循 pattern 属性的语义,也就是说,该正则表达式必须匹配整个控件值。

A directive that adds regex pattern validation to controls marked with the pattern attribute. The regex must match the entire control value. The directive is provided with the NG_VALIDATORS multi-provider list.

参见

NgModules

选择器

属性

属性说明
@Input()
pattern: string | RegExp

Tracks changes to the pattern bound to this directive.

说明

添加模式(pattern)验证器

Adding a pattern validator

下面的例子演示了如何为一个带有 ngModel 绑定的输入框添加模式验证器。

The following example shows how to add a pattern validator to an input attached to an ngModel binding.

<input name="firstName" ngModel pattern="[a-zA-Z ]*">
      
      <input name="firstName" ngModel pattern="[a-zA-Z ]*">
    

方法

A lifecycle method called when the directive's inputs change. For internal use only.

ngOnChanges(changes: SimpleChanges): void
      
      ngOnChanges(changes: SimpleChanges): void
    
参数
changes SimpleChanges

A object of key/value pairs for the set of changed inputs.

返回值

void

Method that validates whether the value matches the the pattern requirement.

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

ValidationErrors | null

Registers a callback function to call when the validator inputs change.

registerOnValidatorChange(fn: () => void): void
      
      registerOnValidatorChange(fn: () => void): void
    
参数
fn () => void

The callback function

返回值

void