[英]Jquery password strength checker code improvment
I've done a part of this code to change the color of the progress bar regarding it's value.我已经完成了这段代码的一部分来更改进度条的颜色关于它的值。 It goes from red to green regarding the password strength.
关于密码强度,它从红色变为绿色。 I think there is a way better methods to have it done.
我认为有更好的方法来完成它。 However it works perfectly.
然而它完美地工作。 Can someone check it to see if there is a cleaner way to acheive it please?
有人可以检查一下是否有更清洁的方法来实现它吗?
Here is the JavaScript code:这是 JavaScript 代码:
var pass = document.getElementById("password")
pass.addEventListener('keyup', function() {
checkPassword(pass.value)
})
function checkPassword(password) {
var strengthBar = document.getElementById("strength")
var strength = 0;
if (password.match(/[a-zA-Z0-9][a-zA-Z0-9]+/)) {
strength += 1
}
if (password.match(/[~<>?]+/)) {
strength += 1
}
if (password.match(/[!@£$%^&*()]+/)) {
strength += 1
}
if (password.length > 5) {
strength += 1
}
if (password.length > 8) {
strength += 1
}
switch(strength) {
case 0:
strengthBar.value = 0
$('#strength').removeClass('red')
break
case 1:
strengthBar.value = 20
$('#strength').addClass('red')
$('#strength').removeClass("orange")
break
case 2:
strengthBar.value = 40
$('#strength').removeClass("red")
$('#strength').addClass('orange')
$('#strength').removeClass("yellow")
break
case 3:
strengthBar.value = 60
break
case 4:
strengthBar.value = 80
$('#strength').removeClass("orange")
$('#strength').addClass('yellow')
$('#strength').removeClass("green")
break
case 5:
strengthBar.value = 100
$('#strength').removeClass("yellow")
$('#strength').addClass('green')
break
}
}
HTML code for testing purpose: HTML 代码用于测试目的:
<h1>Progress</h1>
<label>Enter password</label>
<input style="width: 200px" type="text" autocomplete="none" id="password">
<progress id="strength" class="strength" value="0" max="100"> 32% </progress>
CSS that goes with: CSS 与:
progress.strength {
-webkit-appearance: none;
appearance: none;
}
progress.strength::-webkit-progress-value {
background;
}
progress.red::-webkit-progress-value {
background:red;
}
progress.orange::-webkit-progress-value {
background:rgb(255, 153, 0);
}
progress.yellow::-webkit-progress-value {
background:rgb(229, 255, 0);
}
progress.green::-webkit-progress-value {
background:rgb(53, 252, 13);
}
Use an array of regular expressions to test against, and clean them up by using the case-insensitive flag and \d
instead of [0-9]
.使用一组正则表达式进行测试,并使用不区分大小写的标志和
\d
而不是[0-9]
来清理它们。 From there, you can determine the total strenth by iterating over the array and testing each.从那里,您可以通过遍历数组并测试每个来确定总强度。
Instead of assigning the .value
for each possibility, multiply the strength
by 20. Eg 2 * 20
results in 40
.不要为每种可能性分配
.value
,而是将strength
乘以 20。例如2 * 20
结果为40
。
Use an array of classes corresponding to each strength value.使用对应于每个强度值的类数组。 Overwrite the prior classes completely in advance instead of trying to figure out the previous color.
提前完全覆盖之前的类,而不是试图找出之前的颜色。
const colors = ['', 'red', 'orange', 'orange', 'yellow', 'green'];
const patterns = [
/[a-z\d][a-z\d]{2,}/i,
/[~<>?]+/,
/[!@£$%^&*()]+/,
/.{6}/,
/.{9}/,
];
const strength = patterns.reduce((count, pattern) => count + pattern.test(password), 0);
strengthBar.value = strength * 20;
$('#strength')
.removeClass(colors.join(' '))
.addClass(colors[strength]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.