How To Use Emailvalidator Package In Angular?
Last Updated :
23 Aug, 2024
The email validator is a simple and popular npm package used to validate email addresses in JavaScript applications. Integrating it into an Angular application can help users provide valid email addresses. Basically the email validator provides a simple and efficient way to validate the email address. In this article, we explain how to integrate the email-validator npm package into our application with related examples and outputs for reference.
Project Preview
Project PreviewPrerequisites
Steps To Use emailvalidator in Angular
Here we provide a step by step process to use emailvalidator in angular and we provide related example and output and also we provide source code for your reference.
Step 1 : Create Angular Application
For create new angular project. Use below command with project name. Here emailValidatorApp is the project name.
ng new emailValidatorApp
Step 2 : Install email-validator package
Once project is created successfully. Now Install the email validator package by using below command in the project directory.
npm install email-validator
Folder Structure
Folder StructureDependencies
"dependencies": {
"@angular/animations": "^18.0.0",
"@angular/common": "^18.0.0",
"@angular/compiler": "^18.0.0",
"@angular/core": "^18.0.0",
"@angular/forms": "^18.0.0",
"@angular/platform-browser": "^18.0.0",
"@angular/platform-browser-dynamic": "^18.0.0",
"@angular/platform-server": "^18.0.0",
"@angular/router": "^18.0.0",
"@angular/ssr": "^18.0.0",
"email-validator": "^2.0.4",
"express": "^4.18.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
}
Step 3 : Design the UI
Once required dependencies are installed now its time to design user interface to validate the given email address. For responsive UI design we use Bootstrap framework as a CDN. And we integrate CDN links with index.html of the Angular Project.
index.html
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>EmailValidatorApp</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
</body>
</html>
Once Bootstrap Framework is integrated. Now we start the UI design for validating the email. Below we provide the source code for your reference.
App Component
Once Basic User Interface is completed Now we need develop the logic for validating the given email by using email-validator npm package.Below we provide that source code for your reference. In email-validator npm package we have a method called validate(). This method take email address as input.
HTML
<!--src/app/app.component.html-->
<div class="p-5">
<div class="container p-4 bg-success mt-5" style="max-width: 600px;">
<h2 class="text text-white mb-4 text-center">Email Validator</h2>
<input class="form-control mt-5" type="email" [(ngModel)]="email" placeholder="Enter your email">
<button class="btn btn-light mt-3 text-success" style="font-weight: 700;"
(click)="validateEmail()">Validate
Email</button>
<p *ngIf="isValidEmail === true" class="text text-white mt-3 mb-2">Email is valid!</p>
<p *ngIf="isValidEmail === false" class="text text-white mt-3 mb-2">Email is invalid!</p>
</div>
</div>
JavaScript
//src/app/app.component.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import * as EmailValidator from 'email-validator';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, FormsModule, CommonModule],
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'emailValidatorApp';
email: string = '';
isValidEmail: boolean | null = null;
validateEmail() {
this.isValidEmail = EmailValidator.validate(this.email);
}
}
JavaScript
//src/app/app.config.ts
import { ApplicationConfig, provideZoneChangeDetection, importProvidersFrom } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideClientHydration } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes),
provideClientHydration(),
importProvidersFrom(FormsModule)
]
};
Step 4: Run the Application
Once development is completed. Now Its time to run the application by using below command. And the Angular application runs on port number 4200 by default.
ng serve
Output
Similar Reads
How to use Mat-Dialog in Angular ?
Introduction:Angular Material is a UI component library that is developed by the Angular team to build design components for desktop and mobile web applications. In order to install it, we need to have angular installed in our project, once you have it you can enter the below command and can downloa
3 min read
How To Use @Injectable Decorator In Angular?
In Angular, the @Injectable decorator is used to mark a class as available for dependency injection. This allows Angular to create and manage instances of this class and inject it into other components, services, or other classes.In this article, we will see how to use the @Injectable decorator in a
3 min read
How To Use Reactive Forms in Angular?
In Angular, the forms are an important part of handling user input and validation. Reactive Forms offers a model-driven approach that provides greater control and flexibility by managing form controls, validation, and data binding directly within the component class. Core ComponentsFormGroup: Repres
5 min read
How To Use OnChanges In Angular?
Angular is a powerful framework for building dynamic web applications, and one of its core features is the ability to manage and react to changes in component data. One of the key lifecycle hooks in Angular for responding to changes in input properties is the OnChanges lifecycle hook. This hook is h
3 min read
How to use Angular Material in Angular 17?
Angular Material is a comprehensive UI component library designed specifically for Angular applications. With its large collection of pre-built components and seamless integration with Angular, Angular Material simplifies the process of creating visually appealing and responsive user interfaces. In
2 min read
How to make an http call in angular
Angular, with its robust framework and extensive ecosystem, offers you powerful tools for building modern web applications. One such tool is the HttpClient module, which provides a seamless way to make HTTP calls and interact with RESTful APIs or backend services. In this article, we'll explore maki
3 min read
How to Use & Create custom directive in Angular?
Directives in Angular are nothing but the classes that allow us to add and modify the behavior of elements. Using directives in angular we can modify the DOM (Document Object Module) styles, handle user functionality, and much more. Table of Content Custom DirectivesSteps to create the DirectiveBene
3 min read
How to create class in Angular Project?
In Angular, TypeScript is the primary language for writing code. One of the fundamental concepts in TypeScript is classes, which allow you to define blueprints for creating objects with properties and methods. In this article, we'll explore how to create classes in Angular projects and understand th
2 min read
How to Validate Data in AngularJS ?
In this article, we will know to validate the data in Angular JS, along with understanding the various ways to validate the data through the examples. AngularJS allows client-side form validation. Form validation is necessary because it ensures that the data in the form is correct, complete, and are
10 min read
How to use AuthGuard For Angular 17 routes?
In Angular applications, it is often necessary to protect certain routes to prevent unauthorized access. The Angular Router provides a feature called Route Guards, which allows you to control access to routes based on specific conditions. One of the commonly used Route Guards is AuthGuard, which che
6 min read