How to create Template Driven Form Validation in Angular 8?

How to create Template Driven Form Validation in Angular 8?

In this angular tutorial, you'll learn step by step to create Template Driven Form Validation in Angular 8

Here is a Angular 8 template driven form validation example. I would like to give you example of how to create template driven form in Angular 8. I will give you simple example of template driven form validation in Angular 8.

Angular 8 provide forms and they provide way to handle user input using ngModel, ngSubmit. Angular 8 provide Template-driven froms and using Template Driven Forms you can create very simple and basic level form.

If you have simple and basic form in your Angular 8 application then i will prefer to use Template Driven Forms in angular. Here I write simple example of Template Driven Forms with validation in Angular 8.

You need to follow bellow step to create template driven form in angular 8.

Step 1: Install Angular App

Here, in this step you need to create new ng app for this demo. if you have already created then don't create new Angular 8 App.

ng new my-new-app

Step 2: Import FormsModule

If you want to create form in angular app then you need to import FormsModule from @angular/forms library. so let's add following code to app.module.ts file.

src/app/app.module.ts

import { BrowserModule } from '@angular/platform-browser';

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

import { FormsModule } from '@angular/forms';



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



@NgModule({

  declarations: [

    AppComponent

  ],

  imports: [

    BrowserModule,

    FormsModule

  ],

  providers: [],

  bootstrap: [AppComponent]

})

export class AppModule { }

Step 3: Form with ngModel

In this step, we will write code of html form with ngModel. so add following code to app.component.html file.

src/app/app.component.html

<h1>Template Driven Forms Validation in Angular 8 Example - ItSolutionStuff.com</h1>



<form #contactForm="ngForm" (ngSubmit)="submit(contactForm.value)">



  <div class="form-group">

      <label for="firstName">First Name</label>

      <input required minlength="3" maxlength="10" ngModel name="firstName" type="text" #firstName="ngModel" class="form-control" id="firstName">

      <div class="alert alert-danger" *ngIf="firstName.touched && !firstName.valid">

          <div *ngIf="firstName.errors.required">First Name is required.</div>

          <div *ngIf="firstName.errors.minlength">First Name is minimum {{ firstName.errors.minlength.requiredLength }} character.</div>

          <div *ngIf="firstName.errors.maxlength">First Name is maximum 10 character.</div>

      </div>

  </div>



  <div class="form-group">

      <label for="lastName">Last Name</label>

      <input required ngModel name="lastName" type="text" #lastName="ngModel" class="form-control" id="lastName">

      <div class="alert alert-danger" *ngIf="lastName.touched && !lastName.valid">

          Last Name is required.

      </div>

  </div>



  <div class="form-group">

      <label for="comment">Comment</label>

      <textarea required ngModel #comment="ngModel" name="comment" id="comment" cols="30" rows="10" class="form-control"></textarea>

      <div class="alert alert-danger" *ngIf="comment.touched && !firstName.valid">

          Comment is required.

      </div>

  </div>



  <button class="btn btn-primary" type="submit" [class.disabled]="!contactForm.valid">Submit</button>



</form>

Step 4: updated Ts File

In ts file. we will write submit() and get all input fields values. so let's add following code to app.component.ts file.

src/app/app.component.ts

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



@Component({

  selector: 'app-root',

  templateUrl: './app.component.html',

  styleUrls: ['./app.component.css']

})

export class AppComponent {



  submit(form){

    var firstName = form.firstName;

    console.log(firstName);



    var lastName = form.lastName;

    console.log(lastName);



    var comment = form.comment;

    console.log(comment);

  }



}

Now you can run your application using following command:

ng serve

I hope it can help you...

Angular

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Install Angular - Angular Environment Setup Process

Install Angular in easy step by step process. Firstly Install Node.js & npm, then Install Angular CLI, Create workspace and Deploy your App.

Basics of Angular: Part-1

What is Angular? What it does? How we implement it in a project? So, here are some basics of angular to let you learn more about angular. Angular is a Typesc

Angular Sass: How To Use Sass In Angular 9 Tutorial

Sass in Angular is an extension of CSS that allows you to use things like variables, nested rules, inline imports. Angular supports Sass, CSS, and Less.

How to Upgrade Angular 8 To Angular 9

Angular is currently the most popular JavaScript framework and is used by several expert developers when developing single-page applications or powerful web apps. It has become quite crucial for the developers of Angular to add more features to the framework and fix the bugs . Let's learn how to upgrade Angular 8 to Angular 9 using CLI.

Visualizing data with NGX-Charts in Angular

Data Science, Data Analytics, Big Data, these are the buzz words of today's world. A huge amount of data is being generated and analyzed every day. So communica