Challenge - 5 Problems
Angular Validators Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a required validator is applied to an empty input?
Consider an Angular form control with the
Validators.required applied. What will be the validation status if the input is empty?Angular
this.form = new FormGroup({ name: new FormControl('', Validators.required) }); console.log(this.form.controls['name'].valid);
Attempts:
2 left
💡 Hint
Think about what 'required' means for a form control value.
✗ Incorrect
The Validators.required ensures the input is not empty. If the input is empty, the control is invalid.
📝 Syntax
intermediate2:00remaining
Which option correctly applies minLength validator to a form control?
You want to require that a username input has at least 5 characters. Which code correctly applies the
Validators.minLength?Attempts:
2 left
💡 Hint
Remember how to call a validator function with a parameter.
✗ Incorrect
The Validators.minLength is a function that takes a number and returns a validator function. It must be called as Validators.minLength(5).
🔧 Debug
advanced2:00remaining
Why does this pattern validator not work as expected?
Given this form control with a pattern validator to accept only digits:
Why might it fail to validate a string of digits?
new FormControl('', Validators.pattern('[0-9]+'))Why might it fail to validate a string of digits?
Attempts:
2 left
💡 Hint
Think about what the pattern matches by default without anchors.
✗ Incorrect
Without anchors, the pattern matches anywhere in the string. To ensure the entire input matches digits only, use '^' and '$' anchors.
❓ state_output
advanced2:00remaining
What is the validation error object when minLength fails?
If a form control has
Validators.minLength(4) and the input is 'abc', what is the value of control.errors?Angular
const control = new FormControl('abc', Validators.minLength(4)); console.log(control.errors);
Attempts:
2 left
💡 Hint
Check the exact error key and properties returned by Angular's minLength validator.
✗ Incorrect
The error object key is 'minlength' (all lowercase), and it contains requiredLength and actualLength properties.
🧠 Conceptual
expert3:00remaining
How do multiple validators combine in Angular forms?
If a form control has
Validators.required and Validators.pattern('^[a-z]+$') applied together, what is the validation result when the input is an empty string?Angular
const control = new FormControl('', [Validators.required, Validators.pattern('^[a-z]+$')]); console.log(control.errors);
Attempts:
2 left
💡 Hint
Think about which validator fails first and how Angular reports errors.
✗ Incorrect
When the input is empty, Validators.required fails, so the error object contains only { required: true }. The pattern validator is not checked if required fails.