Validation in ngForm

  • Below example disables the submit button if the validation fails
  • Highlights the field which has failed validation
  • Shows the validation message

app.component.html

<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>

<script src="//code.jquery.com/jquery-2.1.3.min.js"></script>

<div class="container">
    <div class="row">
        <div class="col-md-3"></div>
        <div class="card border-primary mb-3">
          <div class="card-header" style="background-color: #007BFF; color : white">
            Registeration Form
          </div>
          <div class="card-body">
              <form (ngSubmit)="onSubmit()" #f=ngForm>
                  <div class="form-row">
                      <div class="form-group">
                        <input
                          type="text"
                          class="form-control form-control-sm col-md-12"
                          id="fullName"
                          placeholder="Name"
                          ngModel
                          name="fullName"
                          required><br/>
                          <input
                          type="text"
                          class="form-control form-control-sm col-md-12"
                          id="emailId"
                          placeholder="Email Id"
                          ngModel
                          name="emailId"
                          required email
                          #email="ngModel">
                          <span class="help-block" *ngIf="email.invalid &amp;&amp; email.touched">Please enter a valid email!</span>
                      </div>
                  </div>
                  <div style="text-align:center">
                    <button type="submit" [disabled]="!f.valid" class="btn btn-primary" >Click Me</button>
                </div>
                </form>
          </div>
        </div>
      </div>
</div>

app.component.ts

import { Component, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  @ViewChild('f', {static: false}) registerFrom: NgForm;

  title = 'RegForm';
  onSubmit() {
    console.log(this.registerFrom); // Complete JSON is printed
  }
}

app.component.css

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

app.module.ts

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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

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

Leave a Comment