Angular Forms and Validations

Angular Forms and Validations

04 Jul 2025
Intermediate
10K Views
14 min read
Learn with an interactive course and practical hands-on labs

Angular Course Free with Certificate - Beginner Level

Angular Forms are techniques to handle user input through HTML forms in a structured and efficient way. Form Validation is the process of ensuring that user input meets certain rules (like required fields, email formats, min/max length) before the form can be submitted. Angular Form Validations validate the user input for accuracy and completeness.

You can use forms for various activities like user login or signup, booking a flight, placing an order, etc.Without mastering Angular Forms and Validation, your app could be full of hidden bugs!

types of Angular Forms

In this Angular tutorial, you will learn about Angular forms and validation, Angular built-in form validation, Angular form validation example, and different ways of creating forms in Angular. Angular provides two ways to create forms: Template-Driven and Model-Driven.
Read More: What is Angular?

What are Angular Forms and Validation?

Angular Forms provide a way to manage user input, handle form data, and validate that data in Angular applications. They allow developers to easily build forms that collect and process user input, either through HTML templates (template-driven forms) or programmatically in the component class (model-driven, reactive forms).

Angular Form Validation is the process of checking user input in a form to make sure it meets certain rules or requirements before the data is submitted or processed. Angular Form Validations validate the user input for accuracy and completeness. All validations are performed on the client side.

This ensures that users provide correct and complete information. For example:

  1. Name cannot be empty.
  2. Email must be in a valid format.
  3. Passwords must be at least 8 characters.
  4. Age must be a number between 18 and 60.

Angular Form Built-in Validation

Angular provides built-in validators and allows you to write custom validators to implement any kind of rule.

ValidatorWhat they Does
requiredField must not be empty
minlength / Validators.minLength()Minimum number of characters
maxlength/Validators.maxLength()Maximum number of characters
pattern / Validators.pattern()Matches a regex pattern
email / Validators.emailValid email format

Example of Angular Form Validation

Here’s a very simple Angular Reactive Form Validation example:

// my-form.component.ts
import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';

@Component({
  selector: 'app-my-form',
  templateUrl: './my-form.component.html'
})
export class MyFormComponent {
  myForm = new FormGroup({
    email: new FormControl('', [Validators.required, Validators.email])
  });

  onSubmit() {
    console.log(this.myForm.value);
  }
}
Here, we create a form group called myForm.
Inside this form, 
  • We add 1 field called email
  • We add 2 validation rules to this field:
  1. Validators. required → The email field cannot be empty.
  2. Validators.email → The email must be in a valid format (like abc@gmail.com).

<!-- my-form.component.html -->
<form [formGroup]="myForm" (ngSubmit)="onSubmit()">  <label>Email:</label>
  <input formControlName="email" placeholder="Enter your email">  <div *ngIf="myForm.get('email')?.invalid && myForm.get('email')?.touched">
    <small *ngIf="myForm.get('email')?.errors?.['required']">
      Email is required.
    </small>
    <small *ngIf="myForm.get('email')?.errors?.['email']">
      Invalid email format.
    </small>
  </div>  <button type="submit" [disabled]="myForm.invalid">Submit</button></form>
What this does:
  • The form is connected to myForm.
  • The input box is bound to the email field.
  • If the user enters an invalid email, a message "Invalid email" is shown.
  • When the user clicks Submit, it runs the onSubmit() method (you can define it in your component).

Angular Form Building Blocks

The basic building blocks of an Angular form are:

  • FormsGroup
  • FormControl
  • FormArray
  • Validations

Angular Form Building Blocks

FormsGroup
  • Groups multiple form controls into one object, representing the entire form structure.
FormControl
  • Represents a single form field (input, select, etc.), and tracks its value and validation status.
FormArray
  • Manages a dynamic array of form controls or groups, useful for handling lists of inputs.
Validations
  • Functions that define validation rules for form fields, such as required, minLength, email, or custom rules.

Angular Form and Form Controls States

Angular Forms and their controls do change in their states as the user starts interaction with the form input controls. This state transition is helpful to get information about the form and its input control state. Based on the get information you can show or hide error messages and even you can check the validity of the form.

Angular Form and Form Controls States

States Transition

  1. untouched => touched
  2. invalid => valid
  3. pristine => dirty

Read More: Best Angular Interview Question and Answer

Angular Form Validations

Angular Form Validations are based on HTML5 validations. Angular Form Validations validate the user input for accuracy and completeness. All validations are performed on the client side.

Angular Forms Built-In Validation

  1. required
  2. minlength
  3. maxlength
  4. pattern
  5. email – supports in Ng4
  6. min
  7. max

Types of Angular Forms

Angular offers two distinct approaches for creation and management of forms efficiently depending upon the complexity of the form and the level of control required. These approaches are flexible, robust, and allow developers to implement validation, dynamic controls, and reactive behaviors. Let’s explore the different types of Angular forms.
  1. Template-driven Forms
  2. Model Driven (Reactive Forms)

Template-Driven Form

In template-driven forms, we don't create Angular form control objects but Angular directives create them for us using the information from our data binding configuration. We don't have to push and pull data values because Angular handles that for you using the ngModel directive and Angular updates the mutable data model according to user changes as they happen while using the form.

  • Template-driven Form is set up and configured in HTML Code.
  • Template-driven Form is easy to use and suits simple forms.
  • Template-driven Form uses directives (ngForm, ngModel) and reference names (#refName) for creating forms.
  • Module Required: FormsModule
Template Driven Form

Model-Driven Form (Reactive Forms)

The model-driven form is set up and configured in the component class.

  • It is based on a reactive style of programming where you use the underlying APIs FormControl and FormGroup that track the value and validation status.
  • Reactive forms offer ease of testing and validation.
Model Driven Form

Template-Driven vs. Model-Driven Form

Template Driven
Model-Driven Form
The form is set up and configured in HTML Code.
The form is set up and configured in the component class using FormBuilder.
Easy and suited for simple form.
Flexible and suited for complex form.
Two-way data binding.
No data binding(immutable data model).
Automatically track form and input element state.
Reactive Transformation that can react to changes in data across the app.
The form is passed to the component class via ngSubmit().
The form can be accessed in the component class without passing it via ngSubmit().
Unit Testing is complex.
Unit Testing is easy.

Neither of them is better than the other. Template-driven and Model-driven are two different architectural paradigms with their pros and cons so we can choose the approach that works best for you or according to our business requirements. This way, we are free to decide to use both in the same application accordingly.

Read More: Angular Model-Driven (Reactive) Forms

Read More:

Conclusion

Angular Forms and Validation are essential tools for creating reliable, user-friendly web applications. Whether you choose Template-driven or Reactive Forms, Angular offers the flexibility to handle simple to complex form requirements with ease. Delving into Angular Training can offer in-depth insights into leveraging Angular for building robust and interactive forms.

Building easy-to-use forms requires design and user experience skills, as well as a framework with support for two-way data binding, change tracking, validation, and error handling such as Angular.

FAQs

In Angular, there are two types of forms: template-driven and reactive.

Validation is an automated process where a computer checks if a user input is sensible and meets the program's requirements. There are six categories of validation that can be carried out on fields and data types, these are. Range check. Length check. Type check

NgModel is a directive that creates the FormControl instance from a domain model and binds it to a form control element

Take our Angular skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Live Training - Book Free Demo
Azure Developer Certification Training
14 Jul
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Azure AI Engineer Certification Training
17 Jul
07:00AM - 08:30AM IST
Checkmark Icon
Get Job-Ready
Certification
Azure AI & Gen AI Engineer Certification Training Program
17 Jul
07:00AM - 08:30AM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack .NET Developer with Gen AI Certification Training
20 Jul
09:30AM - 11:30AM IST
Checkmark Icon
Get Job-Ready
Certification
ASP.NET Core Certification Training
20 Jul
09:30AM - 11:30AM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this