Bird
0
0

function numericValidator(control: AbstractControl): ValidationErrors | null { const valid = /^[0-9]+$/.test(control.value); return valid ?

medium📝 Predict Output Q4 of 15
Angular - Reactive Forms
Given this custom validator code, what will be the validation result for input value '12345'? function numericValidator(control: AbstractControl): ValidationErrors | null { const valid = /^[0-9]+$/.test(control.value); return valid ? null : { numeric: true }; }
A{ numeric: true } (invalid input)
BThrows runtime error
Cundefined
Dnull (valid input)
Step-by-Step Solution
Solution:
  1. Step 1: Understand the validator logic

    The validator tests if the input contains only digits using a regular expression.
  2. Step 2: Test input '12345'

    All characters are digits, so the regex test returns true, making valid true.
  3. Step 3: Return value

    Since valid is true, the function returns null, meaning no validation error.
  4. Final Answer:

    null (valid input) -> Option D
  5. Quick Check:

    Valid numeric input returns null [OK]
Quick Trick: Regex test true means valid, so return null [OK]
Common Mistakes:
MISTAKES
  • Confusing null with error object
  • Assuming regex test returns error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes