Angular 13 Email Validation Example | Angular 13 Tutorial

Share the post

Hello All, In this angular 13 tutorial, We will discuss the Angular 13 email validation example. We would learn How to implement email validation in the angular 13 application. In the last angular 13 tutorials, We had discussed Mobile number validation in angular 13.

In this example, We will take a simple email input element in the HTML file & then validate it. So, Let’s Start…

Steps required to implement angular 13 email validation example

Angular 13 Email Validation Example
Angular 13 Email Validation Example
  • Step1: Create Angular Project
  • Step2: Import ReactiveFormsModule & FormsModule
  • Step3: Update Template
  • Step4: Update TS Code
  • Step5: Update SCSS
  • Step6: Run & See the Output

Step1: Create angular project

Create a new angular project using the below command.

ng new <APPNAME>



Now, move to our root project directory using the following command

cd <APPNAME>



Open the newly created angular project in Visual Studio Code. Use the following command to open the project.

code .

Step2: Import ReactiveFormsModule & FormsModule

Now, We need to import two modules ReactiveFormsModule & FormsModule from angular forms. We import these modules as follows

Update app.module.ts as shown below

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

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

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


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

Step3: Update Template

Now, We have to update our HTML template. We will use the form element which contains the input field for email Id. Then, We will give the FormGroup name which is a valid name for the form. After that, For every input element, We need to give formControlName. In this example, We are only using the Email Id input filed. So, We are using only one formControlName. On the bottom side, We use one button which handles the click event. In this event, We will capture the input field value.

Update the app.component.html as shown below

<form [formGroup]="formGrp" novalidate>
  <div>
    <label>Email ID</label>
    <label class="show-error">*</label><br>
    <input type="email" formControlName="emailctrl" placeholder="Please enter email id here">

    <div *ngIf="emailid['emailctrl'].touched && emailid['emailctrl'].invalid" class="show-error">
      <div *ngIf="emailid['emailctrl'].errors?.['required'] && emailid['emailctrl'].dirty">Please enter email id</div>
      <div *ngIf="emailid['emailctrl'].errors?.['email'] && emailid['emailctrl'].dirty">Please enter valid email id</div>
    </div>
  </div>

  <div>
    <button type="submit" [disabled]="!formGrp.valid" (click)="doSubmit()">Save</button>
  </div>
</form>

Step4: Update TS Code

In our TypeScript file, We need to import FormBuilder, FormGroup, Validators from angular forms.

Update app.component.ts as shown below

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent {

  formGrp: FormGroup;

  constructor(formBuilder: FormBuilder) {
    this.formGrp = formBuilder.group({
      emailctrl: ['', [Validators.required, Validators.email]]
    })
  }

  get emailid(){
    return this.formGrp.controls;
  }

  doSubmit() {
    console.log(this.formGrp.value);
  }

}

Step5: Update SCSS

We are showing the error part using CSS as shown below.

Update app.component.scss

.show-error{
    color: red;
}

Step6: Run & See the Output

Now, Everything is done. Just need to run the angular application & see the output.

Run the Angular application using the below command

ng serve

Output

output
output

1 thought on “Angular 13 Email Validation Example | Angular 13 Tutorial”

Leave a Comment