Skip to content

Commit

Permalink
Fixed #1623 - Add mediumRegex and strongRegex property to Password
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed Oct 30, 2020
1 parent 504eb1e commit 556600c
Showing 1 changed file with 39 additions and 41 deletions.
80 changes: 39 additions & 41 deletions src/components/password/Password.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class Password extends Component {
weakLabel: 'Weak',
mediumLabel: 'Medium',
strongLabel: 'Strong',
mediumRegex: '^(((?=.*[a-z])(?=.*[A-Z]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[A-Z])(?=.*[0-9])))(?=.{6,})',
strongRegex: '^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})',
feedback: true,
tooltip: null,
tooltipOptions: null,
Expand All @@ -31,6 +33,8 @@ export class Password extends Component {
weakLabel: PropTypes.string,
mediumLabel: PropTypes.string,
strongLabel:PropTypes.string,
mediumRegex: PropTypes.string,
strongRegex: PropTypes.string,
feedback: PropTypes.bool,
tooltip: PropTypes.string,
tooltipOptions: PropTypes.object,
Expand All @@ -55,6 +59,8 @@ export class Password extends Component {
this.onOverlayExit = this.onOverlayExit.bind(this);

this.id = this.props.id || UniqueComponentId();
this.mediumCheckRegExp = new RegExp(this.props.mediumRegex);
this.strongCheckRegExp = new RegExp(this.props.strongRegex);
}

showOverlay() {
Expand Down Expand Up @@ -103,29 +109,30 @@ export class Password extends Component {

onKeyup(e) {
if(this.props.feedback) {
let value = e.target.value,
label = null,
meterPos = null;
let value = e.target.value;
let label = null;
let meterPos = null;

if(value.length === 0) {
label = this.props.promptLabel;
meterPos = '0px 0px';
}
else {
var score = this.testStrength(value);

if(score < 30) {
switch (this.testStrength(value)) {
case 1:
label = this.props.weakLabel;
meterPos = '0px -10px';
}
else if(score >= 30 && score < 80) {
break;

case 2:
label = this.props.mediumLabel;
meterPos = '0px -20px';
}
else if(score >= 80) {
break;

case 3:
label = this.props.strongLabel;
meterPos = '0px -30px';
}
break;

default:
label = this.props.promptLabel;
meterPos = '0px 0px';
break;
}

this.setState({
Expand All @@ -144,33 +151,16 @@ export class Password extends Component {
}

testStrength(str) {
let grade = 0;
let val;

val = str.match('[0-9]');
grade += this.normalize(val ? val.length : 1/4, 1) * 25;

val = str.match('[a-zA-Z]');
grade += this.normalize(val ? val.length : 1/2, 3) * 10;
let level = 0;

val = str.match('[!@#$%^&*?_~.,;=]');
grade += this.normalize(val ? val.length : 1/6, 1) * 35;
if (this.strongCheckRegExp.test(str))
level = 3;
else if (this.mediumCheckRegExp.test(str))
level = 2;
else if (str.length)
level = 1;

val = str.match('[A-Z]');
grade += this.normalize(val ? val.length : 1/6, 1) * 30;

grade *= str.length / 8;

return grade > 100 ? 100 : grade;
}

normalize(x, y) {
let diff = x - y;

if(diff <= 0)
return x / y;
else
return 1 + 0.5 * (x / (x + y/4));
return level;
}

bindScrollListener() {
Expand Down Expand Up @@ -222,6 +212,14 @@ export class Password extends Component {
else
this.renderTooltip();
}

if (prevProps.mediumRegex !== this.props.mediumRegex) {
this.mediumCheckRegExp = new RegExp(this.props.mediumRegex);
}

if (prevProps.strongRegex !== this.props.strongRegex) {
this.strongCheckRegExp = new RegExp(this.props.strongRegex);
}
}

componentWillUnmount() {
Expand Down

0 comments on commit 556600c

Please sign in to comment.