@@ -5,6 +5,7 @@ import zxcvbn from 'zxcvbn';
55import PropTypes from 'prop-types' ;
66
77const isTooShort = ( password , minLength ) => password . length < minLength ;
8+ const isTooLong = ( password , maxLength ) => password . length > maxLength ;
89
910export default class ReactPasswordStrength extends Component {
1011 static propTypes = {
@@ -13,6 +14,7 @@ export default class ReactPasswordStrength extends Component {
1314 defaultValue : PropTypes . string ,
1415 inputProps : PropTypes . object ,
1516 minLength : PropTypes . number ,
17+ maxLength : PropTypes . number ,
1618 minScore : PropTypes . number ,
1719 namespaceClassName : PropTypes . string ,
1820 scoreWords : PropTypes . array ,
@@ -25,10 +27,12 @@ export default class ReactPasswordStrength extends Component {
2527 changeCallback : null ,
2628 className : '' ,
2729 defaultValue : '' ,
30+ maxLength : 1000 ,
2831 minLength : 5 ,
2932 minScore : 2 ,
3033 namespaceClassName : 'ReactPasswordStrength' ,
3134 scoreWords : [ 'weak' , 'weak' , 'okay' , 'good' , 'strong' ] ,
35+ tooLongWord : 'too long' ,
3236 tooShortWord : 'too short' ,
3337 userInputs : [ ] ,
3438 }
@@ -64,21 +68,21 @@ export default class ReactPasswordStrength extends Component {
6468 }
6569
6670 handleChange = ( ) => {
67- const { changeCallback, minScore, userInputs, minLength } = this . props ;
71+ const { changeCallback, minScore, userInputs, minLength, maxLength } = this . props ;
6872 const password = this . reactPasswordStrengthInput . value ;
6973
7074 let score = 0 ;
7175 let result = null ;
7276
7377 // always sets a zero score when min length requirement is not met
7478 // avoids unnecessary zxcvbn computations (CPU intensive)
75- if ( isTooShort ( password , minLength ) === false ) {
79+ if ( isTooShort ( password , minLength ) === false && isTooLong ( password , maxLength ) === false ) {
7680 result = zxcvbn ( password , userInputs ) ;
7781 score = result . score ;
7882 }
7983
8084 this . setState ( {
81- isValid : score >= minScore ,
85+ isValid : score >= minScore && ! isTooLong ( password , maxLength ) ,
8286 password,
8387 score,
8488 } , ( ) => {
@@ -93,10 +97,12 @@ export default class ReactPasswordStrength extends Component {
9397 const {
9498 className,
9599 inputProps,
100+ maxLength,
96101 minLength,
97102 namespaceClassName,
98103 scoreWords,
99104 style,
105+ tooLongWord,
100106 tooShortWord,
101107 } = this . props ;
102108
@@ -106,11 +112,11 @@ export default class ReactPasswordStrength extends Component {
106112 className ? className : '' ,
107113 password . length > 0 ? `is-strength-${ score } ` : '' ,
108114 ] ;
109- const strengthDesc = (
110- isTooShort ( password , minLength )
111- ? tooShortWord
112- : scoreWords [ score ]
113- ) ;
115+
116+ let strengthDesc = scoreWords [ score ] ;
117+
118+ if ( isTooShort ( password , minLength ) ) strengthDesc = tooShortWord ;
119+ if ( isTooLong ( password , maxLength ) ) strengthDesc = tooLongWord ;
114120
115121 if ( isValid === true ) {
116122 inputClasses . push ( 'is-password-valid' ) ;
0 commit comments