Bird
0
0

Given this custom validator code, what will be the validation result for input value 'abc123'?

medium📝 component behavior Q13 of 15
Angular - Reactive Forms
Given this custom validator code, what will be the validation result for input value 'abc123'?
function alphaOnly(control: AbstractControl) {
  const valid = /^[a-zA-Z]+$/.test(control.value);
  return valid ? null : { alphaOnly: true };
}
A{ alphaOnly: true }
Btrue
Cnull
Dfalse
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the regex test on input 'abc123'

    The regex /^[a-zA-Z]+$/ matches only letters. 'abc123' contains digits, so test returns false.
  2. Step 2: Determine validator return value

    Since test is false, the validator returns the error object { alphaOnly: true } indicating invalid input.
  3. Final Answer:

    { alphaOnly: true } -> Option A
  4. Quick Check:

    Input with digits fails alphaOnly validator [OK]
Quick Trick: Regex test false means return error object, not null [OK]
Common Mistakes:
MISTAKES
  • Assuming digits pass the regex
  • Confusing true/false return values
  • Expecting null for invalid input

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes