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
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side : On client sid
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read