Angular – How to Handle Password Confirmation Logic

This blog represents code samples and related concepts on how to handle password confirmation logic in an Angular app (Angular 2/Angular 4). Note that the code samples make use of template-driven forms and uses [(ngModel)] for two-way data bindings.

Following are key aspects discussed in this blog:

  • Template code for handling password confirmation logic
  • Component code for handling password confirmation logic
  • Signup model code

In this blog, it is considered that password confirmation logic is a key part of signup form. Thus, a model name Signup is used. However, the logic can also be used in update password form.

Following screenshot represents password confirmation UI related with the code given in this blog:

Figure 1. Password Confirmation Logic in Angular Form

Template Code for handling Password Confirmation Logic

<div class="form-group">
  <label for="password"><strong>Password</strong></label>
  <input type="password" class="form-control" id="password" [(ngModel)]="signup.password" name="password" (blur)="confirmPassword()" required placeholder="Password" #pswd="ngModel"/>
</div>


<div [hidden]="pswd.valid || pswd.pristine" class="alert alert-danger">Password is required</div>


<div class="form-group">
  <label for="cnfpassword"><strong>Confirm password</strong></label>
  <input type="password" class="form-control" id="cnfpassword" [(ngModel)]="passwordConfirmationTxt" required (blur)="confirmPassword()" name="passwordConfirmationTxt"/>
</div>


<div [hidden]="!passwordConfirmationFailed" class="alert alert-danger">Password did not match</div>

Pay attention to some of the following in above code:

  • Usage of (blur) event handler to invoke the method, confirmPassword
  • Usage of template reference variable #pswd for tracking changes to required field, password
  • Usage of passwordConfirmationFailed property to display the message “Password did not match” based on the whether its value is true or flag. Note the usage of [hidden]

Component Code for handling Password Confirmation Logic

export class SignupComponent {
  passwordConfirmationFailed = false;
  passwordConfirmationTxt = '';

  signup = new Signup('', '', '', '');

  confirmPassword() {
    if (this.signup.password === this.passwordConfirmationTxt) {
      this.passwordConfirmationFailed = false;
    } else {
      this.passwordConfirmationFailed = true;
    }
  }
}

Signup Model Code

export class Signup {

  constructor (
    public email: string,
    public name: string,
    public password: string,
    public country: string,
  ) {  }

}

In case you are developing web apps using Spring and Angular, check out my book, Building web apps with Spring 5 and Angular. Grab your ebook today and get started.

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Share
Published by
Ajitesh Kumar

Recent Posts

What is Embodied AI? Explained with Examples

Artificial Intelligence (AI) has evolved significantly, from its early days of symbolic reasoning to the…

1 month ago

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

4 months ago

How to Setup MEAN App with LangChain.js

Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…

4 months ago

Build AI Chatbots for SAAS Using LLMs, RAG, Multi-Agent Frameworks

Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…

4 months ago

Creating a RAG Application Using LangGraph: Example Code

Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…

5 months ago

Building a RAG Application with LangChain: Example Code

The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…

5 months ago