Bird
Raised Fist0
Angularframework~5 mins

Showing validation errors in Angular

Choose your learning style10 modes available
Introduction

Showing validation errors helps users know what they need to fix in a form. It makes forms easier and clearer to use.

When you want to check if a user typed a valid email address.
When you want to make sure a required field is not empty.
When you want to limit the length of a password.
When you want to show messages right after the user interacts with a form field.
Syntax
Angular
import { Component } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';

@Component({
  standalone: true,
  selector: 'app-simple-form',
  template: `
    <input [formControl]="nameControl" aria-describedby="nameError" />
    <div *ngIf="nameControl.invalid && nameControl.touched" id="nameError" role="alert">
      <small *ngIf="nameControl.errors?.['required']">Name is required.</small>
      <small *ngIf="nameControl.errors?.['minlength']">Name must be at least 3 characters.</small>
    </div>
  `
})
export class SimpleFormComponent {
  nameControl = new FormControl('', [Validators.required, Validators.minLength(3)]);
}

Use FormControl with validators to track input and errors.

Show error messages only when the control is invalid and the user has touched it.

Examples
Simple required field validation with error message.
Angular
nameControl = new FormControl('', Validators.required);

// In template:
// <div *ngIf="nameControl.invalid && nameControl.touched">
//   <small *ngIf="nameControl.errors?.['required']">This field is required.</small>
// </div>
Validation for required and email format with specific messages.
Angular
emailControl = new FormControl('', [Validators.required, Validators.email]);

// In template:
// <div *ngIf="emailControl.invalid && emailControl.touched">
//   <small *ngIf="emailControl.errors?.['required']">Email is required.</small>
//   <small *ngIf="emailControl.errors?.['email']">Enter a valid email.</small>
// </div>
Password field with required and minimum length validation.
Angular
passwordControl = new FormControl('', [Validators.required, Validators.minLength(6)]);

// In template:
// <div *ngIf="passwordControl.invalid && passwordControl.touched">
//   <small *ngIf="passwordControl.errors?.['required']">Password is required.</small>
//   <small *ngIf="passwordControl.errors?.['minlength']">Password must be at least 6 characters.</small>
// </div>
Sample Program

This component shows a login form with email and password fields. It shows error messages below each input if the user leaves the field empty or types invalid data. The login button is disabled until both fields are valid.

Angular
import { Component } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';

@Component({
  standalone: true,
  selector: 'app-login-form',
  template: `
    <form>
      <label for="email">Email:</label>
      <input id="email" type="email" [formControl]="emailControl" aria-describedby="emailError" />
      <div *ngIf="emailControl.invalid && emailControl.touched" id="emailError" role="alert" style="color: red;">
        <small *ngIf="emailControl.errors?.['required']">Email is required.</small>
        <small *ngIf="emailControl.errors?.['email']">Please enter a valid email address.</small>
      </div>

      <label for="password">Password:</label>
      <input id="password" type="password" [formControl]="passwordControl" aria-describedby="passwordError" />
      <div *ngIf="passwordControl.invalid && passwordControl.touched" id="passwordError" role="alert" style="color: red;">
        <small *ngIf="passwordControl.errors?.['required']">Password is required.</small>
        <small *ngIf="passwordControl.errors?.['minlength']">Password must be at least 6 characters.</small>
      </div>

      <button type="submit" [disabled]="emailControl.invalid || passwordControl.invalid">Login</button>
    </form>
  `
})
export class LoginFormComponent {
  emailControl = new FormControl('', [Validators.required, Validators.email]);
  passwordControl = new FormControl('', [Validators.required, Validators.minLength(6)]);
}
OutputSuccess
Important Notes

Use aria-describedby and role="alert" for better accessibility so screen readers announce errors.

Only show errors after the user has touched the input to avoid confusion.

Disable submit buttons when the form is invalid to prevent bad submissions.

Summary

Validation errors help users fix mistakes in forms.

Show errors only when the user interacts with the field and it is invalid.

Use Angular's FormControl and Validators to manage validation easily.

Practice

(1/5)
1. In Angular, when should you show validation error messages for a form field?
easy
A. Only after the user has touched or changed the field and it is invalid
B. Immediately when the form loads, regardless of user interaction
C. Only after the form is submitted, ignoring field interaction
D. Always show errors even if the field is valid

Solution

  1. Step 1: Understand user experience best practice

    Showing errors only after user interaction prevents confusion and clutter.
  2. Step 2: Angular validation behavior

    Angular's touched or dirty flags help detect user interaction before showing errors.
  3. Final Answer:

    Only after the user has touched or changed the field and it is invalid -> Option A
  4. Quick Check:

    Show errors after user interaction = D [OK]
Hint: Show errors only when user touched and invalid [OK]
Common Mistakes:
  • Showing errors immediately on page load
  • Showing errors without checking if field is invalid
  • Waiting only for form submit to show errors
2. Which Angular directive is used to check if a form control is invalid and touched in a template?
easy
A. *ngIf="myControl.invalid && myControl.touched"
B. *ngFor="let control of myControl.invalid"
C. *ngSwitchCase="myControl.valid"
D. *ngModel="myControl.errors"

Solution

  1. Step 1: Identify Angular directives for conditionally showing elements

    *ngIf is used to conditionally display content based on an expression.
  2. Step 2: Check correct condition for validation errors

    Use myControl.invalid && myControl.touched to show errors only when invalid and user touched the field.
  3. Final Answer:

    *ngIf="myControl.invalid && myControl.touched" -> Option A
  4. Quick Check:

    *ngIf with invalid and touched = A [OK]
Hint: Use *ngIf with invalid and touched to show errors [OK]
Common Mistakes:
  • Using *ngFor instead of *ngIf for condition
  • Checking valid instead of invalid
  • Using *ngModel incorrectly for validation display
3. Given this Angular template snippet:
<input [formControl]="emailControl">
<div *ngIf="emailControl.invalid && emailControl.touched">
  <small *ngIf="emailControl.errors?.required">Email is required.</small>
  <small *ngIf="emailControl.errors?.email">Invalid email format.</small>
</div>

What will be shown if the user focuses and leaves the input empty?
medium
A. The message 'Invalid email format.' will be displayed
B. No message will be displayed because the field is empty
C. The message 'Email is required.' will be displayed
D. Both messages will be displayed

Solution

  1. Step 1: Analyze validation conditions

    The input is empty and touched, so required error is true, but email error is false because empty triggers required first.
  2. Step 2: Determine which message shows

    Only the required error message is shown because emailControl.errors?.required is true.
  3. Final Answer:

    The message 'Email is required.' will be displayed -> Option C
  4. Quick Check:

    Empty + touched triggers required error = B [OK]
Hint: Empty touched field triggers required error message [OK]
Common Mistakes:
  • Assuming invalid email message shows on empty input
  • Showing no message on touched empty field
  • Showing both messages simultaneously
4. You wrote this Angular code to show a required error:
<div *ngIf="emailControl.errors.required">Email is required.</div>

But the error never shows even when the field is empty and touched. What is the likely problem?
medium
A. You need to add Validators.email to the control
B. You forgot to check if the control is touched before showing errors
C. You should use emailControl.invalid instead of emailControl.errors.required
D. You must use ngModel instead of formControl

Solution

  1. Step 1: Understand when errors should show

    Errors should show only after user interaction, so checking touched is important.
  2. Step 2: Identify missing condition

    The code only checks errors.required but not if the user touched the field, so error stays hidden initially.
  3. Final Answer:

    You forgot to check if the control is touched before showing errors -> Option B
  4. Quick Check:

    Check touched before errors to show messages = C [OK]
Hint: Always check touched before showing validation errors [OK]
Common Mistakes:
  • Showing errors without user interaction check
  • Confusing invalid with errors property
  • Adding unrelated validators like email for required error
5. You want to show validation errors for a password field only if the user has interacted with it and it is invalid. You also want to show a custom message if the password is less than 8 characters. Which Angular template snippet correctly implements this?
hard
A.
<input type="password" [formControl]="passwordControl">
<div *ngIf="passwordControl.invalid">
  <small *ngIf="passwordControl.errors?.minlength">Password must be at least 8 characters.</small>
</div>
B.
<input type="password" [formControl]="passwordControl">
<div *ngIf="passwordControl.errors?.minlength">
  <small>Password must be at least 8 characters.</small>
</div>
C.
<input type="password" [formControl]="passwordControl">
<div *ngIf="passwordControl.touched">
  <small *ngIf="passwordControl.errors?.minlength">Password must be at least 8 characters.</small>
</div>
D.
<input type="password" [formControl]="passwordControl">
<div *ngIf="passwordControl.invalid && passwordControl.touched">
  <small *ngIf="passwordControl.errors?.minlength">Password must be at least 8 characters.</small>
</div>

Solution

  1. Step 1: Ensure errors show only after user interaction

    Check both invalid and touched to avoid showing errors prematurely.
  2. Step 2: Show specific minlength error message

    Use passwordControl.errors?.minlength inside the conditional block to display the custom message.
  3. Final Answer:

    Correctly combines invalid, touched, and minlength error check -> Option D
  4. Quick Check:

    Check invalid and touched with minlength error = A [OK]
Hint: Combine invalid and touched to show specific error messages [OK]
Common Mistakes:
  • Showing errors without checking touched
  • Showing errors on invalid without user interaction
  • Missing specific error condition for minlength
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard