Implemented custom password level check based on regular expressions
parent
f2b4d1b3f3
commit
4bb31f8e71
|
@ -3,6 +3,8 @@ import Vue from 'vue';
|
||||||
export declare class Password extends Vue {
|
export declare class Password extends Vue {
|
||||||
value?: string;
|
value?: string;
|
||||||
promptLabel?: string;
|
promptLabel?: string;
|
||||||
|
mediumCheckExpr?: string;
|
||||||
|
strongCheckExpr?: string;
|
||||||
weakLabel?: string;
|
weakLabel?: string;
|
||||||
mediumLabel?: string;
|
mediumLabel?: string;
|
||||||
strongLabel?: string;
|
strongLabel?: string;
|
||||||
|
|
|
@ -12,6 +12,14 @@ export default {
|
||||||
type: String,
|
type: String,
|
||||||
default: 'Enter a password'
|
default: 'Enter a password'
|
||||||
},
|
},
|
||||||
|
mediumCheckExpr: {
|
||||||
|
type: String,
|
||||||
|
default: '^(((?=.*[a-z])(?=.*[A-Z]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[A-Z])(?=.*[0-9])))(?=.{6,})' // eslint-disable-line
|
||||||
|
},
|
||||||
|
strongCheckExpr: {
|
||||||
|
type: String,
|
||||||
|
default: '^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})' // eslint-disable-line
|
||||||
|
},
|
||||||
weakLabel: {
|
weakLabel: {
|
||||||
type: String,
|
type: String,
|
||||||
default: 'Weak'
|
default: 'Weak'
|
||||||
|
@ -32,34 +40,24 @@ export default {
|
||||||
panel: null,
|
panel: null,
|
||||||
meter: null,
|
meter: null,
|
||||||
info: null,
|
info: null,
|
||||||
|
mediumCheckRegExp: null,
|
||||||
|
strongCheckRegExp: null,
|
||||||
|
mounted() {
|
||||||
|
this.mediumCheckRegExp = new RegExp(this.mediumCheckExpr);
|
||||||
|
this.strongCheckRegExp = new RegExp(this.strongCheckExpr);
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
testStrength(str) {
|
testStrength(str) {
|
||||||
let grade = 0;
|
let level = 0;
|
||||||
let val;
|
|
||||||
|
|
||||||
val = str.match('[0-9]');
|
if (this.strongCheckRegExp.test(str))
|
||||||
grade += this.normalize(val ? val.length : 1/4, 1) * 25;
|
level = 3;
|
||||||
|
else if (this.mediumCheckRegExp.test(str))
|
||||||
|
level = 2;
|
||||||
|
else if (str.length)
|
||||||
|
level = 1;
|
||||||
|
|
||||||
val = str.match('[a-zA-Z]');
|
return level;
|
||||||
grade += this.normalize(val ? val.length : 1/2, 3) * 10;
|
|
||||||
|
|
||||||
val = str.match('[!@#$%^&*?_~.,;=]');
|
|
||||||
grade += this.normalize(val ? val.length : 1/6, 1) * 35;
|
|
||||||
|
|
||||||
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));
|
|
||||||
},
|
},
|
||||||
createPanel() {
|
createPanel() {
|
||||||
this.panel = document.createElement('div');
|
this.panel = document.createElement('div');
|
||||||
|
@ -119,25 +117,26 @@ export default {
|
||||||
let label = null;
|
let label = null;
|
||||||
let meterPos = null;
|
let meterPos = null;
|
||||||
|
|
||||||
if (value.length === 0) {
|
switch (this.testStrength(value)) {
|
||||||
label = this.promptLabel;
|
case 1:
|
||||||
meterPos = '0px 0px';
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
let score = this.testStrength(value);
|
|
||||||
|
|
||||||
if (score < 30) {
|
|
||||||
label = this.weakLabel;
|
label = this.weakLabel;
|
||||||
meterPos = '0px -10px';
|
meterPos = '0px -10px';
|
||||||
}
|
break;
|
||||||
else if (score >= 30 && score < 80) {
|
|
||||||
|
case 2:
|
||||||
label = this.mediumLabel;
|
label = this.mediumLabel;
|
||||||
meterPos = '0px -20px';
|
meterPos = '0px -20px';
|
||||||
}
|
break;
|
||||||
else if (score >= 80) {
|
|
||||||
|
case 3:
|
||||||
label = this.strongLabel;
|
label = this.strongLabel;
|
||||||
meterPos = '0px -30px';
|
meterPos = '0px -30px';
|
||||||
}
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
label = this.promptLabel;
|
||||||
|
meterPos = '0px 0px';
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
vm.meter.style.backgroundPosition = meterPos;
|
vm.meter.style.backgroundPosition = meterPos;
|
||||||
|
|
Loading…
Reference in New Issue