From 556600ceeda09b529e1b20bdbe2769ec4abfc4a1 Mon Sep 17 00:00:00 2001 From: mertsincan Date: Fri, 30 Oct 2020 16:23:26 +0300 Subject: [PATCH] Fixed #1623 - Add mediumRegex and strongRegex property to Password --- src/components/password/Password.js | 80 ++++++++++++++--------------- 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/src/components/password/Password.js b/src/components/password/Password.js index 752ff9a854..950f67450a 100644 --- a/src/components/password/Password.js +++ b/src/components/password/Password.js @@ -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, @@ -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, @@ -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() { @@ -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({ @@ -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() { @@ -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() {