A custom validator is a function you write to check if a form control's value meets specific rules beyond built-in checks. It helps you create your own validation logic.
Click to reveal answer
beginner
How do you apply a custom validator to a form control in Angular?
You add the custom validator function to the control's validators array when creating the form control, usually inside FormBuilder or FormControl constructor.
Click to reveal answer
beginner
What should a custom validator function return if the control value is valid?
It should return null to indicate the value passed the validation.
Click to reveal answer
beginner
What does a custom validator return if the control value is invalid?
It returns an object with a key describing the error and a value with details, for example: { 'myError': true }.
Click to reveal answer
beginner
Why use custom validators instead of built-in validators?
Because sometimes your form needs special rules that Angular's built-in validators don't cover, like checking a username format or matching passwords.
Click to reveal answer
What does a custom validator function receive as input?
AA FormControl object
BA string value
CAn event object
DA boolean flag
✗ Incorrect
A custom validator function receives the FormControl object to access its current value and state.
What should a custom validator return if the control value is valid?
Afalse
Btrue
C{}
Dnull
✗ Incorrect
Returning null means the control passed validation with no errors.
How do you add a custom validator to a FormControl?
ACall it inside ngOnInit
BAdd it to the component's providers
CPass it as the second argument in FormControl constructor
DUse it in the template only
✗ Incorrect
You add custom validators as the second argument (or inside an array) when creating the FormControl.
What is the purpose of the error object returned by a custom validator?
ATo describe what validation failed
BTo reset the form
CTo submit the form
DTo style the input
✗ Incorrect
The error object tells Angular which validation rule failed and can be used to show messages.
Which Angular module provides the tools to create reactive forms with custom validators?
AFormsModule
BReactiveFormsModule
CBrowserModule
DHttpClientModule
✗ Incorrect
ReactiveFormsModule is needed to build reactive forms and use custom validators.
Explain how to create and use a custom validator in Angular reactive forms.
Think about the function signature and how Angular expects validators.
You got /5 concepts.
Describe why and when you would create a custom validator instead of using built-in validators.
Consider cases where built-in validators are not enough.
You got /4 concepts.
Practice
(1/5)
1. What does a custom validator function in Angular return when the input is valid?
The regex /^[a-zA-Z]+$/ matches only letters. 'abc123' contains digits, so test returns false.
Step 2: Determine validator return value
Since test is false, the validator returns the error object { alphaOnly: true } indicating invalid input.
Final Answer:
{ alphaOnly: true } -> Option A
Quick Check:
Input with digits fails alphaOnly validator [OK]
Hint: Regex test false means return error object, not null [OK]
Common Mistakes:
Assuming digits pass the regex
Confusing true/false return values
Expecting null for invalid input
4. Identify the error in this custom validator code snippet:
function minLengthFive(control: AbstractControl) {
if (control.value.length < 5) {
return { minLengthFive: true };
}
}
medium
A. Incorrect error object key name
B. Missing return null for valid input
C. Using control.value.length without checking null
D. Validator should return boolean instead of object
Solution
Step 1: Check for runtime errors
The code accesses control.value.length without checking if control.value is null or undefined, causing TypeError when value is null (common for new FormControls).
Step 2: Angular validator best practice
Always check if (control.value == null) return null; before accessing properties like length. Explicit return null; for valid cases is also recommended.
Final Answer:
Using control.value.length without checking null -> Option C
Quick Check:
control.value can be null causing crash [OK]
Hint: Always check control.value != null before .length [OK]
Common Mistakes:
Not checking control.value for null before .length
Returning undefined instead of null for valid input
Confusing error object keys with validator names
5. You want to create a custom validator that checks if a password contains at least one uppercase letter, one lowercase letter, and one digit. Which of these validator implementations correctly applies this logic?
B. function passwordValidator(control: AbstractControl) {
if (!control.value.match(/[A-Z]/) || !control.value.match(/[a-z]/) || !control.value.match(/[0-9]/)) {
return null;
}
return { passwordStrength: true };
}
C. function passwordValidator(control: AbstractControl) {
const val = control.value;
if (val.length < 8) return { passwordStrength: true };
return null;
}
D. function passwordValidator(control: AbstractControl) {
const val = control.value;
if (val.includes(' ')) return { passwordStrength: true };
return null;
}
Solution
Step 1: Understand password requirements
The password must have at least one uppercase, one lowercase, and one digit character.
Step 2: Analyze each option's logic
function passwordValidator(control: AbstractControl) {
const val = control.value || '';
const hasUpper = /[A-Z]/.test(val);
const hasLower = /[a-z]/.test(val);
const hasDigit = /\d/.test(val);
return hasUpper && hasLower && hasDigit ? null : { passwordStrength: true };
} correctly tests all three conditions and returns null if all pass, error object otherwise. function passwordValidator(control: AbstractControl) {
if (!control.value.match(/[A-Z]/) || !control.value.match(/[a-z]/) || !control.value.match(/[0-9]/)) {
return null;
}
return { passwordStrength: true };
} reverses logic, returning null on failure. The remaining options check unrelated conditions.