Skip to content

Commit

Permalink
feat(ngx-material-password-strength): added onStrengthChanged output
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyNahas committed Apr 22, 2018
1 parent d7c2200 commit b0bc979
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
import {Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges} from '@angular/core';

export enum Colors {
primary = 'primary',
Expand All @@ -19,6 +19,9 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
@Input()
externalError: boolean;

@Output()
onStrengthChanged: EventEmitter<number> = new EventEmitter<number>();

containAtLeastEightChars: boolean;
containAtLeastOneLowerCaseLetter: boolean;
containAtLeastOneUpperCaseLetter: boolean;
Expand All @@ -36,7 +39,6 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
}

ngOnChanges(changes: SimpleChanges): void {
console.log('on change: ', changes);
if (changes.externalError && changes.externalError.firstChange) {
this._color = Colors.primary;
return;
Expand All @@ -46,10 +48,9 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
return;
}
this.password && this.password.length > 0 ?
this.calculatePasswordStrength() : this._strength = 0;
this.calculatePasswordStrength() : this.reset();
}


get strength(): number {
return this._strength ? this._strength : 0;
}
Expand All @@ -66,41 +67,51 @@ export class PasswordStrengthComponent implements OnInit, OnChanges {
}

private _containAtLeastEightChars(): boolean {
return this.password.length >= 8;
this.containAtLeastEightChars = this.password.length >= 8;
return this.containAtLeastEightChars;
}

private _containAtLeastOneLowerCaseLetter(): boolean {
return RegExp(/^(?=.*?[a-z])/).test(this.password);
this.containAtLeastOneLowerCaseLetter = RegExp(/^(?=.*?[a-z])/).test(this.password);
return this.containAtLeastOneLowerCaseLetter;
}

private _containAtLeastOneUpperCaseLetter(): boolean {
return RegExp(/^(?=.*?[A-Z])/).test(this.password);
this.containAtLeastOneUpperCaseLetter = RegExp(/^(?=.*?[A-Z])/).test(this.password);
return this.containAtLeastOneUpperCaseLetter;
}

private _containAtLeastOneDigit(): boolean {
return RegExp(/^(?=.*?[0-9])/).test(this.password);
this.containAtLeastOneDigit = RegExp(/^(?=.*?[0-9])/).test(this.password);
return this.containAtLeastOneDigit;
}

private _containAtLeastOneSpecialChar(): boolean {
return RegExp(/^(?=.*?[" !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"])/).test(this.password);
this.containAtLeastOneSpecialChar = RegExp(/^(?=.*?[" !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"])/).test(this.password);
return this.containAtLeastOneSpecialChar;
}

calculatePasswordStrength() {
const requirements: Array<boolean> = [];
const unit = 100 / 5;


requirements.push(
this._containAtLeastEightChars(),
this._containAtLeastOneLowerCaseLetter(),
this._containAtLeastOneUpperCaseLetter(),
this._containAtLeastOneDigit(),
this._containAtLeastOneSpecialChar());

console.log('requirements = ', requirements);

this._strength = requirements.filter(v => v).length * unit;
this.onStrengthChanged.emit(this.strength);
}

console.log('strength = ', this._strength);
reset() {
this._strength = 0;
this.containAtLeastEightChars =
this.containAtLeastOneLowerCaseLetter =
this.containAtLeastOneUpperCaseLetter =
this.containAtLeastOneDigit =
this.containAtLeastOneSpecialChar = false;
}
}

0 comments on commit b0bc979

Please sign in to comment.