DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Rediscovering Angular: Modern Advantages and Technical Stack
  • Angular Input/Output Signals: The New Component Communication
  • Azure Deployment Using FileZilla
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators

Trending

  • How to Use AWS Aurora Database for a Retail Point of Sale (POS) Transaction System
  • Introduction to Retrieval Augmented Generation (RAG)
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  1. DZone
  2. Coding
  3. Frameworks
  4. Implement a Password Strength Meter in Angular 8

Implement a Password Strength Meter in Angular 8

By 
Siddharth Gajbhiye user avatar
Siddharth Gajbhiye
·
Apr. 02, 20 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
22.7K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

In most applications, there is a field while registering to enter a valid password, which should contain at least a number and one special symbol. In this article, we are going to learn how to create a password strength bar that will detail the strength of a given password.

Prerequisite

  • Basic knowledge of Angular.
  • Visual Studio Code must be installed.
  • Angular CLI must be installed.
  • Node must be installed.

Step 1

Let's create a new Angular project, using the following npm command.

Shell
xxxxxxxxxx
1
 
1
ng new passwordStrengthBar

 

Step 2

Now, let's create a new component by using the following command.

Shell
xxxxxxxxxx
1
 
1
ng g c password-strength-bar  

 

Step 3

Now, open the password-strength-bar.component.html file and add the following code in this file.

HTML
xxxxxxxxxx
1
 
1
<div style="margin: 11px;" id="strength" #strength>  
2
  <small>{{barLabel}}</small>  
3
  <ul id="strengthBar">  
4
      <li class="point" [style.background-color]="bar0"></li><li class="point" [style.background-color]="bar1"></li><li class="point" [style.background-color]="bar2"></li><li class="point" [style.background-color]="bar3"></li><li class="point" [style.background-color]="bar4"></li>  
5
  </ul>  
6
</div>  

 

Step 4

Now, open the password-strength-bar.component.ts file and add the following code in this file. 

TypeScript
xxxxxxxxxx
1
75
 
1
import {Component, OnChanges, Input, SimpleChange} from '@angular/core';  
2
  
3
@Component({  
4
  selector: 'app-passoword-strength-bar',  
5
  templateUrl: './passoword-strength-bar.component.html',  
6
  styleUrls: ['./passoword-strength-bar.component.css']  
7
})  
8
export class PassowordStrengthBarComponent implements OnChanges  {  
9
  
10
  @Input() passwordToCheck: string;  
11
  @Input() barLabel: string;  
12
  bar0: string;  
13
  bar1: string;  
14
  bar2: string;  
15
  bar3: string;  
16
  bar4: string;  
17
  
18
  private colors = ['#F00', '#F90', '#FF0', '#9F0', '#0F0'];  
19
  
20
  private static measureStrength(pass: string) {  
21
      let score = 0;  
22
      // award every unique letter until 5 repetitions  
23
      let letters = {};  
24
      for (let i = 0; i< pass.length; i++) {  
25
      letters[pass[i]] = (letters[pass[i]] || 0) + 1;  
26
      score += 5.0 / letters[pass[i]];  
27
      }  
28
      // bonus points for mixing it up  
29
      let variations = {  
30
      digits: /\d/.test(pass),  
31
      lower: /[a-z]/.test(pass),  
32
      upper: /[A-Z]/.test(pass),  
33
      nonWords: /\W/.test(pass),  
34
      };  
35
  
36
      let variationCount = 0;  
37
      for (let check in variations) {  
38
      variationCount += (variations[check]) ? 1 : 0;  
39
      }  
40
      score += (variationCount - 1) * 10;  
41
      return Math.trunc(score);  
42
  }  
43
  
44
private getColor(score: number) {  
45
  let idx = 0;  
46
  if (score > 90) {  
47
    idx = 4;  
48
  } else if (score > 70) {  
49
    idx = 3;  
50
  } else if (score >= 40) {  
51
    idx = 2;  
52
  } else if (score >= 20) {  
53
    idx = 1;  
54
  }  
55
  return {  
56
    idx: idx + 1,  
57
    col: this.colors[idx]  
58
  };  
59
}  
60
  
61
  ngOnChanges(changes: {[propName: string]: SimpleChange}): void {  
62
      var password = changes['passwordToCheck'].currentValue;  
63
      this.setBarColors(5, '#DDD');  
64
      if (password) {  
65
          let c = this.getColor(PassowordStrengthBarComponent.measureStrength(password));  
66
          this.setBarColors(c.idx, c.col);  
67
      }  
68
  }  
69
  private setBarColors(count, col) {  
70
      for (let _n = 0; _n < count; _n++) {  
71
          this['bar' + _n] = col;  
72
      }  
73
  }  
74
  
75
}  

 

Step 5

Now, open the password-strength-bar.component.css file and add the following code in this file. 

CSS
xxxxxxxxxx
1
19
 
1
ul#strengthBar {  
2
    display:inline;  
3
    list-style:none;  
4
    margin:0;  
5
    margin-left:15px;  
6
    padding:0;  
7
    vertical-align:2px;  
8
}  
9
.point:last {  
10
    margin:0 !important;  
11
}  
12
.point {  
13
    background:#DDD;  
14
    border-radius:2px;  
15
    display:inline-block;  
16
    height:5px;  
17
    margin-right:1px;  
18
    width:20px;  
19
}  

 

Step 6

Now, open the app.component.html file and add the following code. 

HTML
xxxxxxxxxx
1
33
 
1
<h3>Password Strength Bar</h3>  
2
<div class="container">  
3
    <div class="row">  
4
        <div class="col-md-8 col-md-offset-2">  
5
  
6
            <div class="panel panel-default">  
7
                <div class="panel-body">  
8
                    <form class="form-horizontal" method="" action="">  
9
  
10
                        <div class="form-group">  
11
                            <label class="col-md-4 control-label">Email</label>  
12
                            <div class="col-md-6">  
13
                                <input type="email" class="form-control" name="email" value="">  
14
                            </div>  
15
                        </div>  
16
                        <div class="form-group">  
17
                            <label class="col-md-4 control-label">Password</label>  
18
                            <div class="col-md-6">  
19
                                <input type="password" class="form-control"  
20
                                    id="password" name="password" placeholder="Enter password"  
21
                                    [(ngModel)]="account.password" #password="ngModel" minlength="5" maxlength="50"  
22
                                    required>  
23
                         
24
                                <app-passoword-strength-bar [passwordToCheck]="account.password" [barLabel]="barLabel">  
25
                                </app-passoword-strength-bar>  
26
                            </div>  
27
                        </div>  
28
                    </form>  
29
                </div>  
30
            </div>  
31
        </div>  
32
    </div>  
33
</div>  

  

Step 7

Now, open the app.component.ts file and add the following code in this file.

TypeScript
xxxxxxxxxx
1
19
 
1
import { Component, OnInit } from '@angular/core';  
2
  
3
@Component({  
4
  selector: 'app-root',  
5
  templateUrl: './app.component.html',  
6
  styleUrls: ['./app.component.css']  
7
})  
8
export class AppComponent implements OnInit {  
9
  public account = {  
10
    password: null  
11
  };  
12
  public barLabel: string = "Password strength:";  
13
  
14
  constructor() { }  
15
  
16
  ngOnInit() {  
17
  
18
  }  
19
}  

 

Step 8

Now, open the app.module.ts file and add the following code in this file.

TypeScript
xxxxxxxxxx
1
19
 
1
import { BrowserModule } from '@angular/platform-browser';  
2
import { NgModule } from '@angular/core';  
3
import { FormsModule } from '@angular/forms';  
4
import { AppComponent } from './app.component';  
5
import { PassowordStrengthBarComponent } from './passoword-strength-bar/passoword-strength-bar.component';  
6
  
7
@NgModule({  
8
  declarations: [  
9
    AppComponent,  
10
    PassowordStrengthBarComponent  
11
  ],  
12
  imports: [  
13
    BrowserModule,  
14
    FormsModule,  
15
  ],  
16
  providers: [],  
17
  bootstrap: [AppComponent]  
18
})  
19
export class AppModule { }  


Step 9

Now, let's run the project by using  npm start or ng serve command and check the output:

Password strength low

Password strength low


Password strength medium

Password strength medium


Password strength fair

Password strength fair


Password strength good

Password strength good


Password strength great

Password strength great

Summary

In this article, We have learned how we can create a password strength bar in Angular 8 applications.

Please give your valuable feedback/comments/questions about this article. Please let me know if you liked and understood this article, and how I could improve upon it.

Password strength AngularJS

Opinions expressed by DZone contributors are their own.

Related

  • Rediscovering Angular: Modern Advantages and Technical Stack
  • Angular Input/Output Signals: The New Component Communication
  • Azure Deployment Using FileZilla
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!