-
Notifications
You must be signed in to change notification settings - Fork 14
/
passwordStrengthMeter.js
97 lines (86 loc) · 3.05 KB
/
passwordStrengthMeter.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
// this line calls an imaginary class strength meter which will contain all styles for frontend.
const strengthMeter = document.getElementById('strength-meter');
const passwordInput = document.getElementById('password-input');
const reasonsContainer = document.getElementById('reasons');
// this will call the function whenever we change our passsword in real time
// and also will display all the outcomes.
passwordInput.addEventListener('input', updateStrengthMeter)
updateStrengthMeter()
function updateStrengthMeter() {
const weakness = calculatePassswordStrength(passwordInput.value)
let strength = 100
reasonsContainer.innerHTML = ''
weaknesses.forEach(weakness => {
if (weakness == null) return
strength -= weakness.deduction;
const messageElement = document.createElement('div')
messageElement.innerText = weakness.message;
reasonsContainer.appendChild()
})
strengthMeter.style.setProperty('--strength', strength);
}
// Main function which will calculate the strength of the passwords.
function calculatePassswordStrength(password){
const weaknesses = [];
weaknesses.push(lengthWeakness(password));
weaknesses.push(lowercaseWeakness(password));
weaknesses.push(uppercaseWeakness(password));
weaknesses.push(numberWeakness(password));
weaknesses.push(specialCharacterWeakness(password));
weaknesses.push(repeatCharacterWeakness(password));
return weaknesses;
}
// For length weakness issues.
function lengthWeakness(password){
// length of your entered password gets stored here!
const length = password.length;
// if your entered password is too short, it will return the error message.
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 specialCharacterWeakness(password){
return characterTypeWeakness(password, /[^0-9a-zA-Z\s]/g, 'special characters')
}
function characterTypeWeakness(password, regex, type) {
const matches = password.match(regex) || [];
if (matches.length === 0) {
return {
message: `Your message has no ${type}`,
deduction: 20
}
}
if (matches.length <= 2) {
return {
message: `Your password could use more ${type}`,
deduction: 5
}
}
}
function repeatCharacterWeakness(password){
const matches = password.match(/(.)\1/g) || [];
if(matches.length > 0) {
return {
message: 'Your password has repeat characters',
deduction: matches.length * 10
}
}
}