Angular · Middle · Question 35 of 75
How do you write a custom synchronous validator?
export const noSpaces: ValidatorFn = (control) =>
/\s/.test(control.value ?? "") ? { noSpaces: true } : null;- ✓Correct answer: A function that takes a control and returns an errors object or
nullwhen valid - 2A class that throws an exception when the value is invalid
- 3A pipe added to the input
Explanation
Add it to the control's validators. The error key appears in control.errors, so the template can check control.hasError('noSpaces').