Introduction

In most applications, there is a field while registering to enter a valid password that should contain at least a digit a number and one special symbol. In this article, we are going to learn how to create a password strength bar which will show whether the entered password is weak, good, or strong.
Prerequisites
  • Basic knowledge of Angular
  • Visual Studio Code must be installed
  • Angular CLI must be installed
  • Node JS must be installed
Step 1
Lets create a new Angular project using the following NPM command:
  1. ng new passwordStrengthBar
Step 2
Now, let's create a new component by using the following command:
  1. ng g c password-strength-bar
Step 3
Now, open the password-strength-bar.component.html file and add the following code in the file:
  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:
  1. import {Component, OnChanges, Input, SimpleChange} from '@angular/core';
  2. @Component({
  3. selector: 'app-passoword-strength-bar',
  4. templateUrl: './passoword-strength-bar.component.html',
  5. styleUrls: ['./passoword-strength-bar.component.css']
  6. })
  7. export class PassowordStrengthBarComponent implements OnChanges {
  8. @Input() passwordToCheck: string;
  9. @Input() barLabel: string;
  10. bar0: string;
  11. bar1: string;
  12. bar2: string;
  13. bar3: string;
  14. bar4: string;
  15. private colors = ['#F00', '#F90', '#FF0', '#9F0', '#0F0'];
  16. private static measureStrength(pass: string) {
  17. let score = 0;
  18. // award every unique letter until 5 repetitions
  19. let letters = {};
  20. for (let i = 0; i< pass.length; i++) {
  21. letters[pass[i]] = (letters[pass[i]] || 0) + 1;
  22. score += 5.0 / letters[pass[i]];
  23. }
  24. // bonus points for mixing it up
  25. let variations = {
  26. digits: /\d/.test(pass),
  27. lower: /[a-z]/.test(pass),
  28. upper: /[A-Z]/.test(pass),
  29. nonWords: /\W/.test(pass),
  30. };
  31. let variationCount = 0;
  32. for (let check in variations) {
  33. variationCount += (variations[check]) ? 1 : 0;
  34. }
  35. score += (variationCount - 1) * 10;
  36. return Math.trunc(score);
  37. }
  38. private getColor(score: number) {
  39. let idx = 0;
  40. if (score > 90) {
  41. idx = 4;
  42. } else if (score > 70) {
  43. idx = 3;
  44. } else if (score >= 40) {
  45. idx = 2;
  46. } else if (score >= 20) {
  47. idx = 1;
  48. }
  49. return {
  50. idx: idx + 1,
  51. col: this.colors[idx]
  52. };
  53. }
  54. ngOnChanges(changes: {[propName: string]: SimpleChange}): void {
  55. var password = changes['passwordToCheck'].currentValue;
  56. this.setBarColors(5, '#DDD');
  57. if (password) {
  58. let c = this.getColor(PassowordStrengthBarComponent.measureStrength(password));
  59. this.setBarColors(c.idx, c.col);
  60. }
  61. }
  62. private setBarColors(count, col) {
  63. for (let _n = 0; _n < count; _n++) {
  64. this['bar' + _n] = col;
  65. }
  66. }
  67. }
Step 5
Now, open the password-strength-bar.component.css file and add the following code:
  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 in this file:
  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. <div class="panel panel-default">
  6. <div class="panel-body">
  7. <form class="form-horizontal" method="" action="">
  8. <div class="form-group">
  9. <label class="col-md-4 control-label">Email</label>
  10. <div class="col-md-6">
  11. <input type="email" class="form-control" name="email" value="">
  12. </div>
  13. </div>
  14. <div class="form-group">
  15. <label class="col-md-4 control-label">Password</label>
  16. <div class="col-md-6">
  17. <input type="password" class="form-control"
  18. id="password" name="password" placeholder="Enter password"
  19. [(ngModel)]="account.password" #password="ngModel" minlength="5" maxlength="50"
  20. required>
  21. <app-passoword-strength-bar [passwordToCheck]="account.password" [barLabel]="barLabel">
  22. </app-passoword-strength-bar>
  23. </div>
  24. </div>
  25. </form>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. </div>
Step 7
Now, open the app.component.ts file and add the following code:
  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'app-root',
  4. templateUrl: './app.component.html',
  5. styleUrls: ['./app.component.css']
  6. })
  7. export class AppComponent implements OnInit {
  8. public account = {
  9. password: null
  10. };
  11. public barLabel: string = "Password strength:";
  12. constructor() { }
  13. ngOnInit() {
  14. }
  15. }
Step 8
Now, open the app.module.ts file and add the following code:
  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. @NgModule({
  7. declarations: [
  8. AppComponent,
  9. PassowordStrengthBarComponent
  10. ],
  11. imports: [
  12. BrowserModule,
  13. FormsModule,
  14. ],
  15. providers: [],
  16. bootstrap: [AppComponent]
  17. })
  18. export class AppModule { }
Step 9
Now let's run the project by using 'npm start' or 'ng serve' command and check the output.
How To Check Password Strength Meter In Angular 8
How To Check Password Strength Meter In Angular 8
How To Check Password Strength Meter In Angular 8
How To Check Password Strength Meter In Angular 8

Summary

In this article, we 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 can improve upon it.