0
0
Angularframework~20 mins

Custom validators in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Validator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What happens when a custom validator returns null?
In Angular, a custom validator function returns null. What does this mean for the form control's validity?
Angular
function myValidator(control: AbstractControl): ValidationErrors | null {
  return null;
}
AThe control is valid because null means no errors.
BThe control is invalid because null means an error occurred.
CThe control's value is reset to empty.
DThe validator is ignored and does not run.
Attempts:
2 left
💡 Hint
Think about what Angular expects from a validator when there are no problems.
📝 Syntax
intermediate
2:00remaining
Identify the correct custom validator syntax
Which of the following is the correct way to write a synchronous custom validator function in Angular?
Afunction myValidator(control: AbstractControl): boolean { return control.value.length > 3; }
Bfunction myValidator(control: AbstractControl) { if (control.value.length > 3) return null; else return { tooShort: true }; }
Cfunction myValidator(control: AbstractControl): ValidationErrors | null { return control.value.length > 3 ? null : { tooShort: true }; }
Dfunction myValidator(control: AbstractControl): string | null { return control.value.length > 3 ? 'valid' : null; }
Attempts:
2 left
💡 Hint
Check the return type and syntax carefully.
state_output
advanced
2:00remaining
What is the validation state after applying this custom validator?
Given this custom validator and a form control with value 'abc', what is the control's errors property after validation?
Angular
function startsWithAValidator(control: AbstractControl): ValidationErrors | null {
  return control.value.startsWith('A') ? null : { startsWithA: true };
}

const control = new FormControl('abc', startsWithAValidator);
const errors = control.errors;
Anull
B{ startsWithA: true }
C{ required: true }
Dundefined
Attempts:
2 left
💡 Hint
Check if the string 'abc' starts with 'A'.
🔧 Debug
advanced
2:30remaining
Why does this custom validator cause a runtime error?
This custom validator throws an error when the form control value is empty. Why?
Angular
function positiveNumberValidator(control: AbstractControl): ValidationErrors | null {
  if (control.value == null || control.value <= 0) {
    return { notPositive: true };
  }
  return null;
}
ABecause the validator must return a Promise, not null or object.
BBecause the validator must check control.valid instead of control.value.
CBecause the function is missing a return statement.
DBecause control.value can be null or undefined, and comparing with <= causes an error.
Attempts:
2 left
💡 Hint
Think about what happens if control.value is empty or missing.
🧠 Conceptual
expert
3:00remaining
How to create a reusable parameterized custom validator?
You want to create a custom validator that checks if a control's value length is at least a minimum number, passed as a parameter. Which approach correctly creates this reusable validator?
A
function minLengthValidator(min: number): ValidatorFn {
  return (control: AbstractControl): ValidationErrors | null =&gt; {
    return control.value.length &gt;= min ? null : { minLength: { requiredLength: min, actualLength: control.value.length } };
  };
}
B
function minLengthValidator(control: AbstractControl, min: number): ValidationErrors | null {
  return control.value.length &gt;= min ? null : { minLength: { requiredLength: min, actualLength: control.value.length } };
}
C
const minLengthValidator = (min: number, control: AbstractControl): ValidationErrors | null =&gt; {
  return control.value.length &gt;= min ? null : { minLength: { requiredLength: min, actualLength: control.value.length } };
};
D
function minLengthValidator(min: number): ValidationErrors {
  if (control.value.length &lt; min) {
    return { minLength: true };
  }
  return null;
}
Attempts:
2 left
💡 Hint
Remember Angular expects a function returning a validator function for parameterized validators.