In this tutorial, you will learn how to use angular Template Driven Forms Validation with step by step tutorial.

so, let’s get started,

What are Angular Forms?

  • Angular Forms allows you to get the values the user entered, It Will also allow you to check if a form is valid or not.
  • It will also allow you conditionally change the form input look by putting some red border around invalid controls.

Also read, 

Angular actually offers two approaches when it comes to handing forums.

Difference between Template Driven  and Reactive Forms in angular

1.Template Driven Forms:-

In this type of form you simply set up your form in the HTML template and angular will automatically identify the structure of your forms and its control which makes it easy for you to get started quickly.

2.Reactive Forms:-

A reactive Form is a Form which created programmatically and synchronized with the DOM.

In reactive form approaches is little more complex approaches than Template Driven where you actually define the structure of the form in typescript code.

Template Driven Forms Validation Example

Step-1: Import formsmodule in app.module.ts file and also import in imports array.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';

@NgModule({
  imports:      [ BrowserModule, FormsModule ],
  declarations: [ AppComponent],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Step- 2: Add the following code in app.component.html 

Please enter a valid email!

Step – 3: Add the following code in app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
 
  genders = ['male', 'female'];
  
  userData = {
    username: '',
    email: '',
    country: '',
    gender: ''
  };
  submitted = false;

  
onSubmit(form,formData) {
    console.log('submitted formdata',formData);  
    
    alert('Form submitted successfully');
    
    form.reset();
  }
}

Step-4: Add following code app.component.css

input.ng-invalid.ng-touched {
  border: 1px solid red;
}

See it in action :

View Demo

Conclusion:

Thanks for reading.

Do let me know If you face any difficulties please feel free to comment below we love to help you. if you have any feedback suggestions then please inform us by commenting.

Topics covered:

Found this article helpful?

TutsCoder tutorials are free and ad-light — supported by readers like you. Buy me a coffee (or two ☕☕) as a token of appreciation and help keep Angular & Node.js content coming!

One-time. No subscription. 100% optional. 🙏 Every coffee counts!

Leave a Comment

Your email will not be published. Spam-free zone. ✌️

Available for Projects

Need Help With Your
Angular or Node.js Project?

7+ years of MEAN Stack experience. I build scalable Angular 21 apps, Node.js APIs, and SaaS products — delivered on time, every time.

7+ Years MEAN Stack Angular 21 + Nx Expert 20+ Projects Delivered Remote / Freelance