Bird
0
0

Given this Angular template snippet:

medium📝 state output Q13 of 15
Angular - Template-Driven Forms
Given this Angular template snippet:
<input type="text" name="username" ngModel required minlength="4" #usernameRef="ngModel">
<div *ngIf="usernameRef.invalid && usernameRef.touched">
  <small *ngIf="usernameRef.errors?.required">Username is required.</small>
  <small *ngIf="usernameRef.errors?.minlength">Minimum 4 characters.</small>
</div>

What will happen if the user types "abc" and then clicks outside the input?
AThe message "Minimum 4 characters." will be shown
BNo message will be shown because the input is valid
CThe message "Username is required." will be shown
DBoth messages will be shown
Step-by-Step Solution
Solution:
  1. Step 1: Check input value against validation rules

    The input value "abc" has 3 characters, which is less than the minlength of 4, so minlength validation fails.
  2. Step 2: Determine which error messages show on invalid and touched

    The input is not empty, so required is satisfied. Since usernameRef.invalid and usernameRef.touched are true, only the minlength error message appears.
  3. Final Answer:

    The message "Minimum 4 characters." will be shown -> Option A
  4. Quick Check:

    Input too short = minlength error shown [OK]
Quick Trick: Check which validation fails for input value and touched state [OK]
Common Mistakes:
MISTAKES
  • Showing required error when input is not empty
  • Showing no message when input is invalid and touched
  • Showing both errors when only one applies

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes