@@ -5,6 +5,7 @@ import zxcvbn from 'zxcvbn';
5
5
import PropTypes from 'prop-types' ;
6
6
7
7
const isTooShort = ( password , minLength ) => password . length < minLength ;
8
+ const isTooLong = ( password , maxLength ) => password . length > maxLength ;
8
9
9
10
export default class ReactPasswordStrength extends Component {
10
11
static propTypes = {
@@ -13,6 +14,7 @@ export default class ReactPasswordStrength extends Component {
13
14
defaultValue : PropTypes . string ,
14
15
inputProps : PropTypes . object ,
15
16
minLength : PropTypes . number ,
17
+ maxLength : PropTypes . number ,
16
18
minScore : PropTypes . number ,
17
19
namespaceClassName : PropTypes . string ,
18
20
scoreWords : PropTypes . array ,
@@ -25,10 +27,12 @@ export default class ReactPasswordStrength extends Component {
25
27
changeCallback : null ,
26
28
className : '' ,
27
29
defaultValue : '' ,
30
+ maxLength : 1000 ,
28
31
minLength : 5 ,
29
32
minScore : 2 ,
30
33
namespaceClassName : 'ReactPasswordStrength' ,
31
34
scoreWords : [ 'weak' , 'weak' , 'okay' , 'good' , 'strong' ] ,
35
+ tooLongWord : 'too long' ,
32
36
tooShortWord : 'too short' ,
33
37
userInputs : [ ] ,
34
38
}
@@ -64,21 +68,21 @@ export default class ReactPasswordStrength extends Component {
64
68
}
65
69
66
70
handleChange = ( ) => {
67
- const { changeCallback, minScore, userInputs, minLength } = this . props ;
71
+ const { changeCallback, minScore, userInputs, minLength, maxLength } = this . props ;
68
72
const password = this . reactPasswordStrengthInput . value ;
69
73
70
74
let score = 0 ;
71
75
let result = null ;
72
76
73
77
// always sets a zero score when min length requirement is not met
74
78
// avoids unnecessary zxcvbn computations (CPU intensive)
75
- if ( isTooShort ( password , minLength ) === false ) {
79
+ if ( isTooShort ( password , minLength ) === false && isTooLong ( password , maxLength ) === false ) {
76
80
result = zxcvbn ( password , userInputs ) ;
77
81
score = result . score ;
78
82
}
79
83
80
84
this . setState ( {
81
- isValid : score >= minScore ,
85
+ isValid : score >= minScore && ! isTooLong ( password , maxLength ) ,
82
86
password,
83
87
score,
84
88
} , ( ) => {
@@ -93,10 +97,12 @@ export default class ReactPasswordStrength extends Component {
93
97
const {
94
98
className,
95
99
inputProps,
100
+ maxLength,
96
101
minLength,
97
102
namespaceClassName,
98
103
scoreWords,
99
104
style,
105
+ tooLongWord,
100
106
tooShortWord,
101
107
} = this . props ;
102
108
@@ -106,11 +112,11 @@ export default class ReactPasswordStrength extends Component {
106
112
className ? className : '' ,
107
113
password . length > 0 ? `is-strength-${ score } ` : '' ,
108
114
] ;
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 ;
114
120
115
121
if ( isValid === true ) {
116
122
inputClasses . push ( 'is-password-valid' ) ;
@@ -126,6 +132,7 @@ export default class ReactPasswordStrength extends Component {
126
132
< div className = { wrapperClasses . join ( ' ' ) } style = { style } >
127
133
< input
128
134
type = "password"
135
+ maxLength = { maxLength }
129
136
{ ...inputProps }
130
137
className = { inputClasses . join ( ' ' ) }
131
138
onChange = { this . handleChange }
0 commit comments