Cookies concent notice

This site uses cookies from Google to deliver its services and to analyze traffic.
Learn more
Skip to main content
This site is no longer updated.Head to Angular.devHome
/

This is the archived documentation for Angular v17. Please visit angular.dev to see this page for the current version of Angular.

Validator

An interface implemented by classes that perform synchronous validation.

      
      interface Validator {
validate(control: AbstractControl<any, any>): ValidationErrors | null
registerOnValidatorChange(fn: () => void)?: void }

Child interfaces

Methods

Method that performs synchronous validation against the provided control.

      
      validate(control: AbstractControl<any, any>): ValidationErrors | null
    
Parameters
control AbstractControl<any, any>

The control to validate against.

Returns

ValidationErrors | null: A map of validation errors if validation fails, otherwise null.

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

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

The callback function

Returns

void

Usage notes

Provide a custom validator

The following example implements the Validator interface to create a validator directive with a custom error key.

      
      @Directive({
  selector: '[customValidator]',
  providers: [{provide: NG_VALIDATORS, useExisting: CustomValidatorDirective, multi: true}]
})
class CustomValidatorDirective implements Validator {
  validate(control: AbstractControl): ValidationErrors|null {
    return {'custom': true};
  }
}