@@ -22,14 +22,18 @@ export enum Criteria {
2222} )
2323export class MatPasswordStrengthComponent implements OnInit , OnChanges {
2424
25- @Input ( )
26- password : string ;
25+ @Input ( ) password : string ;
26+ @Input ( ) validators : Criteria [ ] = Object . keys ( Criteria ) . map ( key => Criteria [ key ] ) ;
27+ @Input ( ) externalError : boolean ;
2728
28- @Input ( )
29- validators : Criteria [ ] = Object . keys ( Criteria ) . map ( key => Criteria [ key ] ) ;
29+ @Input ( ) enableLengthRule = true ;
30+ @Input ( ) enableLowerCaseLetterRule = true ;
31+ @Input ( ) enableUpperCaseLetterRule = true ;
32+ @Input ( ) enableDigitRule = true ;
33+ @Input ( ) enableSpecialCharRule = true ;
3034
31- @Input ( )
32- externalError : boolean ;
35+ @Input ( ) min = 8 ;
36+ @ Input ( ) max = 30 ;
3337
3438 @Output ( )
3539 onStrengthChanged : EventEmitter < number > = new EventEmitter < number > ( ) ;
@@ -48,18 +52,13 @@ export class MatPasswordStrengthComponent implements OnInit, OnChanges {
4852
4953 private _color : string ;
5054
51- constructor ( ) {
52- this . criteriaMap . set ( Criteria . at_least_eight_chars , RegExp ( / ^ .{ 8 , 63 } $ / ) ) ;
53- this . criteriaMap . set ( Criteria . at_least_one_lower_case_char , RegExp ( / ^ (? = .* ?[ a - z ] ) / ) ) ;
54- this . criteriaMap . set ( Criteria . at_least_one_upper_case_char , RegExp ( / ^ (? = .* ?[ A - Z ] ) / ) ) ;
55- this . criteriaMap . set ( Criteria . at_least_one_digit_char , RegExp ( / ^ (? = .* ?[ 0 - 9 ] ) / ) ) ;
56- this . criteriaMap . set ( Criteria . at_least_one_special_char , RegExp ( / ^ (? = .* ?[ " ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \] ^ _ ` { | } ~ " ] ) / ) ) ;
5755
58- this . passwordFormControl = new FormControl ( '' ,
59- [ ... this . validators . map ( criteria => Validators . pattern ( this . criteriaMap . get ( criteria ) ) ) ] ) ;
56+ constructor ( ) {
57+ this . setRulesAndValidators ( ) ;
6058 }
6159
6260 ngOnInit ( ) : void {
61+ this . setRulesAndValidators ( ) ;
6362 }
6463
6564 ngOnChanges ( changes : SimpleChanges ) : void {
@@ -127,6 +126,27 @@ export class MatPasswordStrengthComponent implements OnInit, OnChanges {
127126 return this . containAtLeastOneSpecialChar ;
128127 }
129128
129+ setRulesAndValidators ( ) {
130+ if ( this . enableLengthRule ) {
131+ this . criteriaMap . set ( Criteria . at_least_eight_chars , RegExp ( `^.{${ this . min } ,${ this . max } $` ) ) ;
132+ }
133+ if ( this . enableLowerCaseLetterRule ) {
134+ this . criteriaMap . set ( Criteria . at_least_one_lower_case_char , RegExp ( / ^ (? = .* ?[ a - z ] ) / ) ) ;
135+ }
136+ if ( this . enableUpperCaseLetterRule ) {
137+ this . criteriaMap . set ( Criteria . at_least_one_upper_case_char , RegExp ( / ^ (? = .* ?[ A - Z ] ) / ) ) ;
138+ }
139+ if ( this . enableDigitRule ) {
140+ this . criteriaMap . set ( Criteria . at_least_one_digit_char , RegExp ( / ^ (? = .* ?[ 0 - 9 ] ) / ) ) ;
141+ }
142+ if ( this . enableSpecialCharRule ) {
143+ this . criteriaMap . set ( Criteria . at_least_one_special_char , RegExp ( / ^ (? = .* ?[ " ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \] ^ _ ` { | } ~ " ] ) / ) ) ;
144+ }
145+
146+ this . passwordFormControl = new FormControl ( '' ,
147+ [ ...this . validators . map ( criteria => Validators . pattern ( this . criteriaMap . get ( criteria ) ) ) ] ) ;
148+ }
149+
130150 calculatePasswordStrength ( ) {
131151 const requirements : Array < boolean > = [ ] ;
132152 const unit = 100 / 5 ;
0 commit comments