-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
121 lines (98 loc) · 3.59 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const strengthMeter = document.getElementById("strength-meter");
const passwordInput = document.getElementById("password-input");
const reasonsContainer = document.getElementById("reasons");
const passwordIconImg = document.getElementById("toggle-password");
passwordIconImg.addEventListener('click', () => {
const inputAttribute = passwordInput.getAttribute('type') === "text" ? "password" : "text";
passwordInput.setAttribute("type", inputAttribute);
//update the icon
if(inputAttribute == "text"){
passwordIconImg.setAttribute("src" , "./icons/show.png");
}else{
passwordIconImg.setAttribute("src" , "./icons/hide.png");
}
});
passwordInput.addEventListener("input", updateStrengthMeter);
updateStrengthMeter(); //calculate the password strength immediately based on the input current password
function updateStrengthMeter() {
if(passwordInput.value == null) return;
const weaknesses = calculatePasswordStrength(passwordInput.value);
console.log(weaknesses);
let strength = 100;
reasonsContainer.innerHTML = '';
weaknesses.forEach(weakness => {
if(weakness == null) return;
strength -= weakness.deduction;
const messageElementDiv = document.createElement('div');
messageElementDiv.innerHTML = weakness.message;
reasonsContainer.appendChild(messageElementDiv);
})
strengthMeter.style.setProperty('--strength', strength);
}
function calculatePasswordStrength(password) {
const weaknesses = [];
weaknesses.push(lengthWeakness(password));
weaknesses.push(lowercaseWeakness(password));
weaknesses.push(uppercaseWeakness(password));
weaknesses.push(numberWeakness(password));
weaknesses.push(specialCharactersWeakness(password));
weaknesses.push(repeatCharactersWeakness(password));
return weaknesses;
}
function lengthWeakness(password) {
const length = password.length;
if (length <= 5) {
return {
message: "Your password is too short",
deduction: 40,
};
}
if (length <= 10) {
return {
message: "Your password could be longer",
deduction: 15,
};
}
}
function lowercaseWeakness(password){
return characterTypeWeakness(password, /[a-z]/g, 'lowercase characters');
}
function uppercaseWeakness(password){
return characterTypeWeakness(password, /[A-Z]/g, 'uppercase characters');
}
function numberWeakness(password){
return characterTypeWeakness(password, /[0-9]/g, 'numbers');
}
function specialCharactersWeakness(password){
// ^ not symbol followed by the conditions
// \s white space
return characterTypeWeakness(password, /[^0-9a-zA-Z\s]/g, 'special characters');
}
function characterTypeWeakness(password, regex, type){
const matches = password.match(regex);
if(matches == null || matches.length === 0){
return {
message: `Your password has no ${type}`,
deduction: 20
}
}
if(matches.length <= 2){
return {
message: `Your password could use more ${type}`,
deduction: 5
}
}
}
function repeatCharactersWeakness(password){
// (.) means anything followed by the same thing \1 . so we have two characters in a row like aa or 11 or more.
// e.g, The length would be 1 for 'aa' and 2 for 'aaaa'. every two set is increased by 1 length
// to have it match for atleast three character set the format is /(.)\1\1/g)
const matches = password.match(/(.)\1\1/g);
if(matches == null) return;
if(matches.length > 0){
return {
message: 'Your password has repeat characters',
deduction: matches.length * 10,
}
}
}