Angular 6 Reactive Forms Validation Tutorial Example

Angular 6 Reactive Forms Validation Tutorial Example From Scratch

Today, We want to share with you Angular 6 Reactive Forms Validation Tutorial Example From Scratch.In this post we will show you Angular 6 Forms Validation Example, hear for Angular 6 – Template-Driven Forms Validation Example we will give you demo and example for implement.In this post, we will learn about with an example.

Angular 6 Reactive Forms Validation Tutorial Example From Scratch

There are the Following The simple About Angular 6 Reactive Forms Validation Tutorial Example From Scratch Full Information With Example and source shortname.

As I will cover this Post with live Working example to develop Required, Email, URL, Dropdown and Min-Max min-length for Angular 6 Reactive Forms Validation, so the some major We are using a simple angular 6 registration HTML form with validation standard fields – for this simple example is following below.

  • Member
  • Email
  • Gender
  • Name
  • Password
  • Webwebsite URL
Read Also:  PHP Remove File from Folder and Database

Step 1 : Make a project

new create angular 6 Project using CMD.

ng g new ng6memberFormValidation

Step 2 : Make member components

member.component.ts

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

@Component({
  selector: 'app-member',
  templateUrl: './member.component.html',
  styleUrls: ['./member.component.css']
})
export class MemberComponent implements OnInit {

  model: any = {};
  members : any;

  constructor() {
   }

  ngOnInit() {
    //Get member list.
    this.members =[
      {"id" :1, "name": "Jaydeep", "shortname":"jd"},
      {"id" :2, "name": "Krunal", "shortname":"kk"},
      {"id" :3, "name": "Ankit", "shortname": "aghu"},
      {"id" :4, "name": "vishal", "shortname": "vs"},
      {"id" :5, "name": "narendramodi", "shortname": "modi"},
      {"id" :6, "name": "sardarvalabhbhaipatel", "shortname": "patidar"},
      {"id" :7, "name": "chandni", "shortname": "chanu"},
      {"id" :8, "name": "sumitra", "shortname": "sumu"},
      {"id" :9, "name": "kajal", "shortname": "kaju"},
    ];

   //Default selected member Id.
   //this.model.member = 5;
  }

  public onSubmitMember() {
    alert('Your Member Details has been submitted successfully. :-)\n\n' + JSON.stringify(this.model))
  }
}

Step 3 : Include member.component.html

simple More And member.component.html

<h1>Register Member</h1>
<b>Angular 6 Reactive Forms Validation Tutorial Example From Scratch</b>
<hr>
<form name="form" (ngSubmit)="f.form.valid && onSubmitMember()" #f="ngForm" novalidate>
  <div class="container pakainfo">
    <label for="name"><b>Member Name</b></label>
    <input type="text" placeholder="Enter MEmber Name" name="name" [(ngModel)]="model.name" #name="ngModel" [ngClass]="{ 'is-invalid': f.submitted && name.invalid }" required>
    <div *ngIf="f.submitted && name.invalid" class="invalid-text">
      <div *ngIf="name.errors.required">Name is required</div>
    </div>

    <label for="email"><b>Email</b></label>
    <input type="text" placeholder="Enter Email" name="email" [(ngModel)]="model.email" #email="ngModel" [ngClass]="{ 'is-invalid': f.submitted && email.invalid }" required email>
    <div *ngIf="f.submitted && email.invalid" class="invalid-text">
      <div *ngIf="email.errors.required">Email is required</div>
      <div *ngIf="email.errors.email">Email must be a valid email address</div>
    </div>

    <label for="gender"><b>Gender</b></label>
    <select placeholder="Select Gender" name="gender" [(ngModel)]="model.gender" #gender="ngModel" [ngClass]="{ 'is-invalid': f.submitted && gender.invalid }" required>
      <option>Male</option>
      <option>Female</option>
      <option>Other</option>
    </select>
    <div *ngIf="f.submitted && gender.invalid" class="invalid-text">
      <div *ngIf="gender.errors.required">Member Gender is required</div>
    </div>
    <br>

    <label for="member"><b>Member</b></label>
    <select placeholder="Select member" name="member" [(ngModel)]="model.member" #member="ngModel" [ngClass]="{ 'is-invalid': f.submitted && member.invalid }" required>
      <option *ngFor="let member of members" [value]="member.id"> {{member.name}}</option>
    </select>
    <div *ngIf="f.submitted && member.invalid" class="invalid-text">
      <div *ngIf="member.errors.required">Member is required</div>
    </div>
    <br>

    <label for="website"><b>Member URL</b></label>
    <input type="url" placeholder="Enter website URL" name="website" pattern="https?://.+" [(ngModel)] ="model.website" #website="ngModel" [ngClass]="{ 'is-invalid': f.submitted && website.invalid }"  required url>
    <div *ngIf="f.submitted && website.invalid" class="invalid-text">
      <div *ngIf="website.errors.required">Member is required</div>
      <div *ngIf="website.errors.pattern">Must be a valid website URL</div>
    </div>

    <label for="psw-repeat"><b>Password</b></label>
    <input type="password" placeholder="Password" name="mempassword" [(ngModel)]="model.mempassword" #mempassword="ngModel" [ngClass]="{ 'is-invalid': f.submitted && mempassword.invalid }" required minlength="8">
    <div *ngIf="f.submitted && mempassword.invalid" class="invalid-text">
      <div *ngIf="mempassword.errors.required">Member Password is required</div>
      <div *ngIf="mempassword.errors.minlength">Member Password must be at least 8 characters</div>
    </div>

    <hr>
    <p>By Making an account you agree to our <a href="#">Terms & Privacy</a>.</p>
    <button type="submit" class="registerbtn">Register</button>

  </div>

  <div class="container signin">
    <p>Already has an account? <a href="#">Sign in</a>.</p>
  </div>
</form>

Step 4 : Angular 6 Routing, Module and FormsModule

Import the Angular 6 FormsModule, Angular 6 RouterModule, Angular 6 MemberComponent, routing for navigate add member page in the Angular 6 NgModule

Read Also:  Angular Form Validation Example Tutorial

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

//Angular 6 Reactive Forms Validation Import
import { AppComponent } from './app.component';
import { MemberComponent } from './member/member.component';

@NgModule({
  declarations: [
    AppComponent,
    MemberComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    RouterModule.forRoot([{
      path:'member-link', component:MemberComponent }
    ])
  ],
  providers: [],
  bootstrap: [AppComponent]
})

export class AppModule { }

Angular 6 Reactive Forms Validation Tutorial Example – Output

Angular 6 Reactive Forms Validation Tutorial Example - output
Angular 6 Reactive Forms Validation Tutorial Example – output

Output looks like…….

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  Angular 6 Form Validation with Message - Angular 6 Live Validation

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Angular 6 Reactive Forms Validation Tutorial Example From Scratch.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *