CodeZettaInterview Hub

Angular · Forms · Mid-Level · Scenario

How do you create a custom validator?

Short Interview Answer

Return a function (AbstractControl) => ValidationErrors | null; async validators return Observable/Promise.

Detailed Explanation

Register on controls via validators array. Cross-field validators belong on the group. Keep validators pure.

Example

const forbidden = (c: AbstractControl) =>
  c.value === 'admin' ? { forbidden: true } : null;

Did you know this answer?