0
0
Angularframework~20 mins

Form validation with template attributes in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Template Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the user submits this Angular template-driven form?

Consider this Angular template snippet for a simple form:

<form #myForm="ngForm" (ngSubmit)="onSubmit(myForm)">
  <input name="email" ngModel required email />
  <button type="submit">Submit</button>
</form>

What will be the state of myForm.valid if the input is empty and the user clicks Submit?

Angular
<form #myForm="ngForm" (ngSubmit)="onSubmit(myForm)">
  <input name="email" ngModel required email />
  <button type="submit">Submit</button>
</form>
AmyForm.valid will be false because the required field is empty.
BmyForm.valid will be true because the form submits anyway.
CmyForm.valid will be undefined because no validation runs on submit.
DmyForm.valid will be true only if the input has any text, even if not an email.
Attempts:
2 left
💡 Hint

Think about what the required attribute does in Angular forms.

📝 Syntax
intermediate
1:30remaining
Which template attribute correctly validates a minimum length of 5 characters?

In Angular template-driven forms, which attribute correctly enforces a minimum length of 5 characters on an input?

Angular
<input name="username" ngModel ... />
Amin="5"
BminLength="5"
Cminlength="5"
Dmin_length="5"
Attempts:
2 left
💡 Hint

Angular uses standard HTML5 validation attributes for length.

🔧 Debug
advanced
2:30remaining
Why does this Angular form input not validate the email correctly?

Look at this input in an Angular template-driven form:

<input name="userEmail" ngModel required type="email" />

Despite entering invalid emails, the form shows userEmail.valid as true. What is the likely cause?

Angular
<input name="userEmail" ngModel required type="email" />
AThe input is missing the <code>email</code> attribute for Angular validation.
BThe <code>type="email"</code> attribute disables Angular validation.
CThe <code>required</code> attribute conflicts with <code>type="email"</code>.
DAngular does not support email validation in template-driven forms.
Attempts:
2 left
💡 Hint

Angular template-driven forms need a specific attribute for email validation.

state_output
advanced
2:30remaining
What is the value of myForm.controls['password'].errors after input?

Given this Angular template-driven form input:

<input name="password" ngModel required minlength="8" />

If the user types "abc" in the password field, what will myForm.controls['password'].errors contain?

Angular
<input name="password" ngModel required minlength="8" />
Anull
B{"minlength": {"requiredLength": 8, "actualLength": 3}}
C{"required": true}
D{"maxlength": {"requiredLength": 8, "actualLength": 3}}
Attempts:
2 left
💡 Hint

Think about what errors Angular reports when minlength is not met.

🧠 Conceptual
expert
3:00remaining
Why use template attributes for validation instead of reactive forms validators?

In Angular, template-driven forms use validation attributes like required and minlength directly in the template. Reactive forms use validator functions in the component code.

What is a key advantage of using template attributes for validation?

AThey support asynchronous validation out of the box.
BThey provide better performance than reactive form validators.
CThey enable validation logic to run on the server automatically.
DThey allow quick, declarative validation without writing extra TypeScript code.
Attempts:
2 left
💡 Hint

Think about simplicity and where validation rules live.