This is the archived documentation for Angular v5. Please visit angular.io to see documentation for the current version of Angular.

PatternValidator

npm Package @angular/forms
Module import { PatternValidator } from '@angular/forms';
Source forms/src/directives/validators.ts

Overview

      
      @Directive({
    selector: '[pattern][formControlName],[pattern][formControl],[pattern][ngModel]',
    providers: [PATTERN_VALIDATOR],
    host: { '[attr.pattern]': 'pattern ? pattern : null' }
})
class PatternValidator implements Validator, OnChanges {
  pattern: string | RegExp
  ngOnChanges(changes: SimpleChanges): void
  validate(c: AbstractControl): ValidationErrors | null
  registerOnValidatorChange(fn: () => void): void
}
    

Selectors

      
      [pattern][formControlName]
[pattern][formControl]
[pattern][ngModel]
    

Inputs

Description

A Directive that adds the pattern validator to any controls marked with the pattern attribute, via the NG_VALIDATORS binding. Uses attribute value as the regex to validate Control value against. Follows pattern attribute semantics; i.e. regex must match entire Control value.

Example

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

Members

      
      pattern: string | RegExp
    

      
      ngOnChanges(changes: SimpleChanges): void
    

      
      validate(c: AbstractControl): ValidationErrors | null
    

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