svrjs-mods-directory/js/passwordStrength.js

35 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2024-12-27 15:05:54 +01:00
document.getElementById('password').oninput = function () {
var password = this.value;
var strengthText = document.getElementById('password-strength');
var strength = getPasswordStrength(password);
strengthText.innerHTML = strength.text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
strengthText.className = strength.class;
};
function getPasswordStrength(password) {
var strength = { text: '', class: '' };
if (password.length < 6) {
strength.text = 'Weak';
strength.class = 'password-weak';
} else if (password.length < 10) {
strength.text = 'Medium';
strength.class = 'password-medium';
} else {
var hasUpperCase = /[A-Z]/.test(password);
var hasLowerCase = /[a-z]/.test(password);
var hasNumbers = /\d/.test(password);
var hasSpecialChars = /[!@#$%^&*(),.?":{}|<>]/.test(password);
if (hasUpperCase && hasLowerCase && hasNumbers && hasSpecialChars) {
strength.text = 'Strong';
strength.class = 'password-strong';
} else {
strength.text = 'Medium';
strength.class = 'password-medium';
}
}
return strength;
}