Introduction

In this article, we will learn about a form with a password input field. We will then add a password strength checker. Please follow the below steps.
Password Restrictions
  1. Must be minimum 6 character
  2. Must be a at least one uppercase
  3. Must be a at least one lowercase
  4. Must be at least one special character
  5. Must be at least one number
We will validate the above rules using a Regular Expression.
Step 1
Let's create an Angular app with the following command:
Password Strength In Angular
Step 2
Open app.module.ts file and add the import of reactiveform.
  1. import { ReactiveFormsModule } from '@angular/forms';
After then add the reactiveformsmodule to imports the array.
  1. imports: [
  2. ReactiveFormsModule,
  3. ],
Step 3
We can use the template-driven form validate. Now open the app.component.ts file and add the following code.
  1. <form >
  2. <div class="form-group">
  3. <input type="password" class="form-control" autocomplete="off" placeholder="Password">
  4. </div>
  5. </form>
Step 4
We are using reactive form validation in Angular, so we need to add formgroup property and formcontrolname in the input field .
  1. <form [formGroup]="passwordform">
  2. </form>
We also add the formcontrolname in input filed.
  1. formControlName="chkpassword"
Step 5
Next, we need to open then app.componet.ts file and add the following reference file.
  1. import { FormGroup, FormBuilder, Validators } from '@angular/forms';
  • The formgroup used the state of formcontrol elements .
  • The Formbuilder has a new helper class called formbuilder.
  • The validators some set of built validators to be used.
Step 6
We need add to the construtor and ngOnInit methods.
  1. ngOnInit()
  2. {
  3. this.passwordform = this.form.group({
  4. chkpassword: ['', Validators.required],
  5. });
Step 7
Next, we can create a child component to be used for some bars and indicate the strength of the password. The bars will automatically update to different colors. Create a component for the following npm command:
Ng g c password
This command automatically adds to app.module.ts file
Step 8
We can open the passwordcheck.component.html file and add the following code:
  1. <div class="strength">
  2. <ul class="barlength">
  3. <li class="checker" [style.background-color]="length0"></li>
  4. <li class="checker" [style.background-color]="length1"></li>
  5. <li class="checker" [style.background-color]="length2"></li>
  6. <li class="checker" [style.background-color]="length3"></li>
  7. </ul>
  8. <br>
  9. <p style="font-size: 15px;">{{message}}</p>
  10. </div>
Step 9
Next, we can open the passwordcheck.component.ts to add the property file.
  1. length0: string;
  2. length1: string;
  3. length2: string;
  4. length3: string;
  5. message=''
Step 10
Go to app.component.html page add the passwordcheck child component by using the component selector.
  1. <app-password></app-password>
Step 11
Let's open the passwordcheck.component.ts file and create a input property called passwordcheck
  1. @Input() public passwordCheck: string;
Add output for strength
  1. @Output() Strength = new EventEmitter<boolean>()
Step 12
Create an array colors to be displayed the bars.
  1. private colors = ['red', 'darkorange', 'green', 'darkgreen'];
Now create a method for check the password strength. Add the following code.
  1. private static Strengths(p) {
  2. let checklen = 0;
  3. const regex = /[$-/:-?{-~!"^_@`\[\]]/g;
  4. const lowerLetters = /[a-z]+/.test(p);
  5. const upperLetters = /[A-Z]+/.test(p);
  6. const numbers = /[0-9]+/.test(p);
  7. const symbols = regex.test(p);
  8. const flags = [lowerLetters, upperLetters, numbers, symbols];
  9. let Passwordmaches = 0;
  10. for (const flag of flags) {
  11. Passwordmaches += flag === true ? 1 : 0;
  12. }
  13. checklen += 2 * p.length + ((p.length >= 10) ? 1 : 0);
  14. checklen += Passwordmaches * 10;
  15. //Check length
  16. checklen = (p.length <= 6) ? Math.min(checklen, 3) : checklen;
  17. checklen = (Passwordmaches === 1) ? Math.min(checklen, 10) : checklen;
  18. checklen = (Passwordmaches === 2) ? Math.min(checklen, 20) : checklen;
  19. checklen = (Passwordmaches === 3) ? Math.min(checklen, 30) : checklen;
  20. checklen = (Passwordmaches === 4) ? Math.min(checklen, 40) : checklen;
  21. return checklen;
  22. }
Step 13
The next step is to implement ngonchanges, so add the following code.
  1. ngOnChanges(changes: { [propName: string]: SimpleChange }): void {
  2. debugger;
  3. const password = changes.passwordCheck.currentValue;
  4. this.setBarColors(4, '#DDD');
  5. if (password) {
  6. const c = this.getColor(PasswordComponent.Strengths(password));
  7. this.setBarColors(c.idx, c.col);
  8. const pwdStrength = PasswordComponent.Strengths(password);
  9. if(pwdStrength>=30)
  10. {
  11. this.Strength.emit(true)
  12. }
  13. else
  14. {
  15. this.Strength.emit(false)
  16. }
Step 14
Inside of the if statement in the ngonchanges, add to switch statements.
  1. switch (c.idx) {
  2. case 1:
  3. this.message = 'Weak'+'(Ex:Hari@12345)';
  4. break;
  5. case 2:
  6. this.message = 'Average' +'(Ex:Hari@12345)';
  7. break;
  8. case 3:
  9. this.message = 'Good' +'(Ex:Hari@12345)';
  10. break;
  11. case 4:
  12. this.message = 'Strong';
  13. break;
  14. }
  15. } else {
  16. this.message = '';
  17. }
  18. }
  19. private getColor(s) {
  20. let idx = 0;
  21. if (s <= 10) {
  22. idx = 0;
  23. } else if (s <= 20) {
  24. idx = 1;
  25. } else if (s <= 30) {
  26. idx = 2;
  27. } else if (s <= 40) {
  28. idx = 3;
  29. } else {
  30. idx = 4;
  31. }
  32. return {
  33. idx: idx + 1,
  34. col: this.colors[idx]
  35. };
  36. }
  37. private setBarColors(count, col) {
  38. for (let n = 0; n < count; n++) {
  39. this['length' + n] = col;
  40. }
  41. }
Step 15
Next, we will open the app.component.html file and add the following code:
  1. <app-password
  2. [passwordCheck]="passwordform.value.password"
  3. (Strength)="passwordValid($event)"
  4. ></app-password>

Summary

In this article, we have learned how to add a child component using selector, @input and @output decoders for component interaction.