Angular - Confirm Password Validation Using Custom Validators
Last Updated :
08 Jan, 2025
To confirm password validation in Angular forms, you can use a custom validator to ensure two password fields (like password and confirmPassword) match. This is especially useful in sign-up or password reset forms where password confirmation is necessary.
Prerequisite
Steps To Implement CustomConfirm Password Validation
Step 1: Install Angular CLI if you haven't installed it already.
npm install -g @angular/cli
Step 2: Create a new Angular application using the following command.
ng new password-validator-app
Folder Structure
Folder StructureDependencies
"dependencies": {
"@angular/animations": "^18.2.0",
"@angular/common": "^18.2.0",
"@angular/compiler": "^18.2.0",
"@angular/core": "^18.2.0",
"@angular/forms": "^18.2.0",
"@angular/platform-browser": "^18.2.0",
"@angular/platform-browser-dynamic": "^18.2.0",
"@angular/router": "^18.2.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.10"
}
Step 3: Create the Component Logic
In app.component.ts, create the form using the FormBuilder service, adding custom validators for password complexity requirements and a validator to confirm that the password and confirm password fields match.
JavaScript
//app.component.ts
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import {
FormBuilder,
FormGroup,
ReactiveFormsModule,
Validators,
AbstractControl,
} from '@angular/forms';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, CommonModule, ReactiveFormsModule],
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'password-validator-app';
passwordForm: FormGroup;
constructor(private fb: FormBuilder) {
this.passwordForm = this.fb.group(
{
password: [
'',
[
Validators.required,
Validators.minLength(6),
this.hasUppercase,
this.hasNumber,
this.hasSpecialCharacter,
],
],
confirmPassword: ['', Validators.required],
},
{ validator: this.passwordMatchValidator }
);
}
// Custom validator to check if passwords match
passwordMatchValidator(form: FormGroup) {
const password = form.get('password')?.value;
const confirmPassword = form.get('confirmPassword')?.value;
if (password !== confirmPassword) {
return { passwordMismatch: true };
}
return null;
}
// Custom validator to check if the password contains at least one uppercase letter
hasUppercase(control: AbstractControl) {
const value = control.value;
if (value && !/[A-Z]/.test(value)) {
return { uppercase: true };
}
return null;
}
// Custom validator to check if the password contains at least one number
hasNumber(control: AbstractControl) {
const value = control.value;
if (value && !/\d/.test(value)) {
return { number: true };
}
return null;
}
// Custom validator to check if the password contains at least one special character
hasSpecialCharacter(control: AbstractControl) {
const value = control.value;
if (value && !/[!@#$%^&*(),.?":{}|<>]/.test(value)) {
return { specialCharacter: true };
}
return null;
}
// Form Submit
onSubmit() {
if (this.passwordForm.valid) {
alert(`Form Submitted Successfully`);
} else {
alert('Please check the form for errors');
}
}
// check if a specific control has a specific error and if it was touched
hasError(controlName: string, errorName: string) {
return (
this.passwordForm.get(controlName)?.hasError(errorName) &&
this.passwordForm.get(controlName)?.touched
);
}
}
In this code
- Password Match Validator (passwordMatchValidator): Verifies that password and confirmPassword fields match.
- Uppercase Validator (hasUppercase): Ensures that the password has at least one uppercase letter.
- Number Validator (hasNumber): Ensures that the password contains at least one number.
- Special Character Validator (hasSpecialCharacter): Ensures that the password includes at least one special character.
Step 4: Create the HTMl template.
In app.component.html, set up the form with the appropriate form controls and error messages
HTML
<!-- app.component.html -->
<h1>Password Confirmation Form</h1>
<form [formGroup]="passwordForm" (ngSubmit)="onSubmit()">
<div>
<label for="password">Password:</label>
<input type="password" id="password" formControlName="password" />
</div>
<!-- Error messages for password field -->
<div *ngIf="hasError('password', 'required')">
<p style="color: red">Password is required</p>
</div>
<div *ngIf="hasError('password', 'minlength')">
<p style="color: red">Password must be at least 6 characters long</p>
</div>
<div *ngIf="hasError('password', 'uppercase')">
<p style="color: red">
Password must contain at least one uppercase letter
</p>
</div>
<div *ngIf="hasError('password', 'number')">
<p style="color: red">Password must contain at least one number</p>
</div>
<div *ngIf="hasError('password', 'specialCharacter')">
<p style="color: red">
Password must contain at least one special character
</p>
</div>
<div>
<label for="confirmPassword">Confirm Password:</label>
<input type="password" id="confirmPassword" formControlName="confirmPassword" />
</div>
<!-- Error messages for confirm password field -->
<div *ngIf="passwordForm.errors?.['passwordMismatch'] && passwordForm.get('confirmPassword')?.touched">
<p style="color: red">Passwords do not match!</p>
</div>
<button type="submit" [disabled]="passwordForm.invalid">Submit</button>
</form>
Step 5: Style the Form.
In app.component.css, apply basic styling to improve the form's appearance.
CSS
/* app.component.css */
h1 {
text-align: center;
}
form {
max-width: 400px;
margin: 0 auto;
}
div {
margin-bottom: 10px;
}
input {
padding: 5px;
width: 100%;
box-sizing: border-box;
}
button {
padding: 5px 10px;
}
p {
color: red;
font-size: 12px;
}
To run the application, use the below command
ng serve
Output
Similar Reads
Password Validation Form Using JavaScript
The password Validation form is used to check the password requirements such as the password must have at least one Uppercase, or lowercase, number, and the length of the password. We can also check this after submitting the form but it's not recommended. We can easily check before submitting the fo
4 min read
Create a Password Validator using Tailwind CSS
Passwords must be strong so that hackers can not hack them easily. The following example shows how to check the password strength of the user input password in Tailwind CSS and jQuery. We will use the validator module to achieve this functionality. We will call the isStrongPassword function and pass
3 min read
How to Validate Data using validator Module in Node.js ?
The Validator module is popular for validation. Validation is necessary to check whether the data is correct or not, so this module is easy to use and validates data quickly and easily. Feature of validator module: It is easy to get started and easy to use.It is a widely used and popular module for
2 min read
How to compare password and confirm password inputs using express-validator ?
Registration or Sign Up on any website always requires a confirmed password input and it must be the same as the password. It is basically to ensure that the user enters the password full of his senses and there is no conflict happening. This functionality can be implemented anywhere in our code lik
4 min read
How to trigger Form Validators in Angular2 ?
In Angular 2, the best way to deal with complex forms is by using Reactive forms. Below we are going to elaborate on how to trigger form validators for login page. In reactive forms, we use FormControl and by using this, we get access to sub-fields of form and their properties. Some of their propert
4 min read
Angular PrimeNG Form Password Templates Component
Angular PrimeNG is an open-source front-end UI library that has many native Angular UI components which help developers to build a fast and scalable web solution. This article will discuss the Angular PrimeNG Form Password Templates Component. The Password Component is used to take the input of sens
3 min read
Angular PrimeNG Form Password Templating Component
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will learn how to use the Form Password Templating Component in Angular PrimeNG.
3 min read
How to validate confirm password using JavaScript ?
In almost every modern website we have seen a login and signup feature, which is surely an important service for both the client and the service provider. When a user signs up for a certain website they have to put in a username a password, to enter the password websites ask them to enter a password
3 min read
Create a Password Validator using ReactJS
Password must be strong so that hackers can not hack them easily. The following example shows how to check the password strength of the user input password in ReactJS. We will use the validator module to achieve this functionality. We will call the isStrongPassword function and pass the conditions a
2 min read
How to Validate Data using express-validator Module in Node.js ?
Validation in node.js can be easily done by using the express-validator module. This module is popular for data validation. There are other modules available in market like hapi/joi, etc but express-validator is widely used and popular among them.Steps to install express-validator module:Â Â You can
3 min read