Help Angular by taking a 1 minute survey!Go to surveyHome

PatternValidator

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.

See also

NgModules

Selectors

Properties

Property Description
@Input()
pattern: string | RegExp

Tracks changes to the pattern bound to this directive.

Description

Adding a pattern validator

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 ]*">
    

Methods

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

ngOnChanges(changes: SimpleChanges): void
      
      ngOnChanges(changes: SimpleChanges): void
    
Parameters
changes SimpleChanges

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

Returns

void

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

validate(control: AbstractControl): ValidationErrors | null
      
      validate(control: AbstractControl): ValidationErrors | null
    
Parameters
control AbstractControl
Returns

ValidationErrors | null

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

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

The callback function

Returns

void