Bird
0
0

How can you combine multiple validations like required, minlength, and pattern in a single Angular template input to validate a password field?

hard📝 component behavior Q9 of 15
Angular - Template-Driven Forms
How can you combine multiple validations like required, minlength, and pattern in a single Angular template input to validate a password field?
A<input name="password" ngModel required min="8" pattern="[A-Za-z\d]{8,}">
B<input name="password" ngModel minlength="8" pattern="[A-Za-z\d]{8,}" required>
C<input name="password" ngModel required minlength="8" pattern="^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$">
D<input name="password" ngModel required minlength="8" pattern="[A-Za-z\d]{6,}">
Step-by-Step Solution
Solution:
  1. Step 1: Combine required, minlength, and pattern attributes

    Use required to force input, minlength="8" for minimum length, and a regex pattern that enforces at least one letter and one digit.
  2. Step 2: Evaluate regex pattern correctness

    uses a regex with lookaheads to ensure letters and digits and minimum length 8. Other options have incorrect min attribute or insufficient pattern.
  3. Final Answer:

    <input name="password" ngModel required minlength="8" pattern="^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$"> -> Option C
  4. Quick Check:

    Combine required, minlength, and pattern for strong validation [OK]
Quick Trick: Use lookahead regex with minlength and required for passwords [OK]
Common Mistakes:
MISTAKES
  • Using 'min' instead of 'minlength' for strings
  • Ignoring lookahead in regex for complex rules
  • Placing 'required' attribute incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes