Tagged: angular 6 reactive forms validation

Angular 6 Form Validation Example Tutorial 0

Angular 6 Form Validation Example Tutorial

Today, We want to share with you Angular 6 Form Validation Example Tutorial.In this post we will show you Angular 6 – Reactive 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 Angular 6 Validation Tutorial Example From Scratch with an example.

Angular 6 Form Validation Example Tutorial

There are the Following The simple About Angular 6 Form Validation Example Tutorial Full Information With Example and source code.

As I will cover this Post with live Working example to develop Angularjs 6 Form Validation Example Tutorial, so the some major files and Directory structures for this example is following below.

Angular Form Validation Tutorial

app
--app.component.html
--app.component.ts
--app.module.ts
-index.html
-main.ts
-polyfills.ts

Angular 6 Reactive Form Validation

Angular 6 Reactive Form Validation

Angular 6 Example

Angular Latest My Previous Post With Source code Read more…..

  1. Angular 6 Folder Project Structure
  2. Angular 6 CLI Installation
  3. Angular 6 Module File
  4. Angular 6 Components
  5. Angular 6 Services
  6. Angular 6 Routing
  7. Angular 6 CSS
  8. Angular 6 Class Binding
  9. Angular 6 Animation
  10. Angular 6 Templating
  11. Angular 6 HTTP Client

Step 1 : Angular 6 Reactive forms

index.html

<!DOCTYPE html>
<html>
<head>
    <base href="/" />
    <title>Angular 6 Form Validation Example Tutorial</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Include bootstrap css -->
    <link href="//netdna.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <app>Loading...</app>
</body>
</html>

Step 2 : Template-driven Angular 6 forms

main.ts

import './polyfills';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule);

Step 3 : polyfills in Angular 6

/polyfills.ts

import 'core-js/es6/reflect';
import 'core-js/es7/reflect';
import 'zone.js/dist/zone';

Step 4 : Angular 6 Create a form with Validation

/app/app.component.html

<!-- Angular 6 with Laravel main app container -->
<div class="pakainfo jumbotron">
    <div class="pakainfo container">
        <div class="pakainfo row">
            <div class="pakainfo col-md-6 offset-md-3">
                <h3>Angular 6 Reactive Form Validation</h3>
                <form [formGroup]="membersForm" (ngSubmit)="onSubmit()">
                    <div class="pakainfo form-group">
                        <label>Member Name</label>
                        <input type="text" formControlName="memberfName" class="form-control pakainfo" [ngClass]="{ 'is-invalid': submitted && validfrm.memberfName.errors }" />
                        <div *ngIf="submitted && validfrm.memberfName.errors" class="invalid-feedback">
                            <div *ngIf="validfrm.memberfName.errors.required">Member Name is required</div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Member Sir Name</label>
                        <input type="text" formControlName="memberlName" class="form-control pakainfo" [ngClass]="{ 'is-invalid': submitted && validfrm.memberlName.errors }" />
                        <div *ngIf="submitted && validfrm.memberlName.errors" class="invalid-feedback">
                            <div *ngIf="validfrm.memberlName.errors.required">Member Sir Name is required</div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Email</label>
                        <input type="text" formControlName="email" class="form-control pakainfo" [ngClass]="{ 'is-invalid': submitted && validfrm.email.errors }" />
                        <div *ngIf="submitted && validfrm.email.errors" class="invalid-feedback">
                            <div *ngIf="validfrm.email.errors.required">member Email is required</div>
                            <div *ngIf="validfrm.email.errors.email">member Email must be a valid email address</div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Password</label>
                        <input type="password" formControlName="password" class="form-control pakainfo" [ngClass]="{ 'is-invalid': submitted && validfrm.password.errors }" />
                        <div *ngIf="submitted && validfrm.password.errors" class="invalid-feedback">
                            <div *ngIf="validfrm.password.errors.required">Password is required</div>
                            <div *ngIf="validfrm.password.errors.minlength">Password must be at least 6 characters</div>
                        </div>
                    </div>
                    <div class="form-group">
                        <button class="btn btn-primary">Signup</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<h2><a href="https://stackblitz.com/edit/angular-6-reactive-form-validation" target="_blank" alt="Angular 6 simple Form Validation Example Tutorial" title="Angular 6 simple Form Validation Example Tutorial">Angular 6 simple Form Validation Demo</a></h2>
<!-- credits -->
<div class="text-center">
    <p>
        <a href="https://pakainfo.com/angular-6-form-validation-tutorial/" target="_top">Angular 6 - Reactive Forms Validation Example</a>
    </p>
    <p>
        <a href="https://pakainfo.com" target="_top">pakainfo.com</a>
    </p>
</div>

Step 5 : Import FormGroup and FormControl in Angular 6

/app/app.component.ts

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
    selector: 'app',
    templateUrl: 'app.component.html'
})

export class AppComponent implements OnInit {
    membersForm: FormGroup;
    submitted = false;

    constructor(private formBuilder: FormBuilder) { }

    ngOnInit() {
        this.membersForm = this.formBuilder.group({
            memberfName: ['', Validators.required],
            memberlName: ['', Validators.required],
            email: ['', [Validators.required, Validators.email]],
            password: ['', [Validators.required, Validators.minLength(6)]]
        });
    }

    // convenience getter for easy access to form fields
    get validfrm() { return this.membersForm.controls; }

    onSubmit() {
        this.submitted = true;

        // stop here if form is invalid
        if (this.membersForm.invalid) {
            return;
        }

        alert('SUCCESS!! :-)\n\n' + JSON.stringify(this.membersForm.value))
    }
}

Step 6 : Import ReactiveFormsModule in Angular 6

/app/app.module.ts

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

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

@NgModule({
  imports: [
    BrowserModule,
    ReactiveFormsModule
  ],
  declarations: [
    AppComponent
  ],
  bootstrap: [AppComponent]
})

export class AppModule { }

Angular 6 simple Form Validation Demo

Read :

Angular 6 CRUD Operations Application Tutorials

Summary

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

I hope you get an idea about Angular 6 Form Validation Example Tutorial.
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.

Angular 6 Reactive Forms Validation Tutorial Example 0

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

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

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 :

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.

Angular 6 Forms Validation Tutorial Example From Scratch 0

Angular 6 Forms Validation Tutorial Example From Scratch

Today, We want to share with you Angular 6 Forms Validation Tutorial Example From Scratch.In this post we will show you angular-6-reactive-form-validation, 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 Angular 6 Forms Simple Nesting and Input Validation with an example.

Angular 6 Forms Validation Tutorial Example From Scratch

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

As I will cover this Post with live Working example to develop Angular 6 Form Validation Example Tutorial , so the some major files and Directory structures for this example is following below.

  • Install Angular 6
  • Move Bootstrap 4 in Angular 6.
  • Angular 6 Add ReactiveFormsModule.
  • Angular 6 Add FormGroup and FormControl.
  • Make a angular HTML form.
  • Import module FormBuilder.
  • Add Validation in angular 6.
  • Listing Angular 6 Validation Errors.

Step #1: Install Angular 6

//Angular CLI 
npm install -g @angular/cli

// create a local project
ng new forms

//Start the application
ng serve --open

Step #2: Angular 6 Include Bootstrap 4

npm install bootstrap 4 --save

src >> styles.css >> import the bootstrap.min.css file.
@import "~bootstrap/dist/css/bootstrap.min.css";

Step #3: Import ReactiveFormsModule in Angular 6.

app.module.ts

// app.module.ts

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

imports: [
  BrowserModule,
  ReactiveFormsModule
],

Step #4: Import Angular 6 FormGroup and FormControl.

app.component.ts

// app.component.ts

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  livecrudAngularFrm = new FormGroup ({
    name: new FormControl()
  });
}

Step #5: Make Html a form.

app.component.html

<h2>Angular 6 Forms</h2>
<form [formGroup]="livecrudAngularFrm">
  <div class="form-group">
    <label class="center-block">Name:
      <input class="form-control" formControlName="name">
    </label>
  </div>
</form>

Save angular 6 the file as well as run this Example go to the: http://localhost:4400/

Step #6: Angular 6 Import FormBuilder module.

app.component.ts

// app.component.ts

import { Component } from '@angular/core';
import { FormControl, FormGroup, FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  livecrudAngularFrm = new FormGroup ({
    name: new FormControl()
  });
  constructor(private fb: FormBuilder) {
    this.setupLiveangularForm();
  }
  setupLiveangularForm() {
    this.livecrudAngularFrm = this.fb.group({
      name: '',
    });
  }
}

Step #7:Angular 6 Insert Validation.

app.component.ts

// app.component.ts

import { FormControl, FormGroup, FormBuilder, Validators } from '@angular/forms';

// app.component.ts

setupLiveangularForm() {
    this.livecrudAngularFrm = this.fb.group({
      name: ['', Validators.required ],
    });
}

Step #8: Angular 6 Form Validation Errors.

app.component.html

<!-- app.component.html -->

<div class="container">
    <h1>
     Angular 6 Forms Validation Tutorial Example From Scratch
    </h1>
    <form [formGroup]="livecrudAngularFrm" novalidate>
          <div class="form-group">
              <label>User Name:</label>
              <input class="form-control" formControlName="name" type="text">
          </div>
          <div *ngIf="livecrudAngularFrm.controls['name'].invalid && (livecrudAngularFrm.controls['name'].dirty || livecrudAngularFrm.controls['name'].touched)" class="alert alert-danger">
              <div *ngIf="livecrudAngularFrm.controls['name'].errors.required">
                User Name is required.
              </div>
          </div>
          <button type="submit"
              [disabled]="livecrudAngularFrm.pristine || livecrudAngularFrm.invalid" class="btn btn-success">
              Save
          </button>
    </form>
    <br />
  </div>

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

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

I hope you get an idea about Angular 6 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.

Angular 6 Form Validation Tutorial Example From Scratch 0

Angular 6 Form Validation Tutorial Example From Scratch

Today, We want to share with you Angular 6 Form Validation Tutorial Example From Scratch.
In this post we will show you Form Validation in Angular 6 with Example, hear for Angular 6 form validation on submit example we will give you demo and example for implement.
In this post, we will learn about with an example.

Angular 6 Form Validation Tutorial Example From Scratch

There are the Following The simple About Angular 6 Form Validation Tutorial with Email validator Full Information With Example and source code.

This is a quick Lovely Post of how to setup first simple HTML form validation in Angular 6 using get Data form-validation.with Custom CSS of the simple example is all done with latest version of the Bootstrap 4 CSS.

Angular 6 Example

Angular Latest My Previous Post With Source code Read more…..

  1. Angular 6 Folder Project Structure
  2. Angular 6 CLI Installation
  3. Angular 6 Module File
  4. Angular 6 Components
  5. Angular 6 Services
  6. Angular 6 Routing
  7. Angular 6 CSS
  8. Angular 6 Class Binding
  9. Angular 6 Animation
  10. Angular 6 Templating
  11. Angular 6 HTTP Client

Template-driven validation

template/student-form-template.component.html

<input id="uname" name="uname" class="pakainfo form-control"
      required minlength="4" appcustomNameForBidden="birthdate"
      [(ngModel)]="student.name" #name="ngModel" >

<div *ngIf="uname.invalid && (uname.dirty || uname.touched)"
    class="alert alert-danger">

  <div *ngIf="uname.errors.required">
    UserName is required.
  </div>
  <div *ngIf="uname.errors.minlength">
    UserName must be at least 5 characters long.
  </div>
  <div *ngIf="uname.errors.customNameForBidden">
    UserName cannot be This birthdate.
  </div>

</div>

angular 6 Reactive form validation

reactive/student-form-reactive.component.ts (validator functions)

ngOnInit(): void {
  this.studentForm = new FormGroup({
    'uname': new FormControl(this.student.uname, [
      Validators.required,
      Validators.minLength(5),
      customNameForBiddenValidator(/birthdate/i) //
    ]),
    'alterEgo': new FormControl(this.student.alterEgo),
    'power': new FormControl(this.student.power, Validators.required)
  });

}

get uname() { return this.studentForm.get('uname'); }

get power() { return this.studentForm.get('power'); }

reactive/student-form-reactive.component.html (uname with error msg)

<input id="uname" class="form-control"
      formControlName="uname" required >

<div *ngIf="uname.invalid && (uname.dirty || uname.touched)"
    class="alert alert-danger">

  <div *ngIf="uname.errors.required">
    UserName is required.
  </div>
  <div *ngIf="uname.errors.minlength">
    UserName must be Min 5 characters long.
  </div>
  <div *ngIf="uname.errors.customNameForBidden">
    UserName cannot be birthdate.
  </div>
</div>

Angular 6 Custom validators

shared/forbidden-name.directive.ts

export function customCLLForBiddenNameValidator(nameRe: RegExp): ValidatorFn {
  return (control: AbstractControl): {[key: string]: any} | null => {
    const dataforget = nameRe.test(control.value);
    return dataforget ? {'customNameForBidden': {value: control.value}} : null;
  };
}

Read :

Summary

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

I hope you get an idea about angular 6 reactive forms validation.
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.