0
0
Angularframework~20 mins

Validators (required, minLength, pattern) in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Validators Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
AThe control throws an error due to missing input.
BThe control is valid because empty string is allowed.
CThe control is invalid because the input is empty.
DThe control is pending validation until user types.
Attempts:
2 left
💡 Hint
Think about what 'required' means for a form control value.
📝 Syntax
intermediate
2: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?
Anew FormControl('', Validators.minLength(5))
Bnew FormControl('', Validators.minLength = 5)
Cnew FormControl('', Validators.minLength[5])
Dnew FormControl('', Validators.minLength{5})
Attempts:
2 left
💡 Hint
Remember how to call a validator function with a parameter.
🔧 Debug
advanced
2:00remaining
Why does this pattern validator not work as expected?
Given this form control with a pattern validator to accept only digits:
new FormControl('', Validators.pattern('[0-9]+'))

Why might it fail to validate a string of digits?
AThe pattern string should be wrapped with start and end anchors: '^' and '$'.
BThe pattern should use double backslashes for digits: '\\d+'.
CThe pattern validator only works with letters, not digits.
DThe pattern must be passed as a RegExp object, not a string.
Attempts:
2 left
💡 Hint
Think about what the pattern matches by default without anchors.
state_output
advanced
2: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);
Anull
B{ 'minlength': { 'requiredLength': 4, 'actualLength': 3 } }
C{ 'minLength': { 'requiredLength': 4, 'actualLength': 3 } }
D{ 'lengthError': true }
Attempts:
2 left
💡 Hint
Check the exact error key and properties returned by Angular's minLength validator.
🧠 Conceptual
expert
3: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);
A{ 'pattern': { 'requiredPattern': '^[a-z]+$', 'actualValue': '' } }
Bnull
C{ 'required': true, 'pattern': { 'requiredPattern': '^[a-z]+$', 'actualValue': '' } }
D{ 'required': true }
Attempts:
2 left
💡 Hint
Think about which validator fails first and how Angular reports errors.