-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
108 lines (87 loc) · 3.17 KB
/
script.js
File metadata and controls
108 lines (87 loc) · 3.17 KB
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
let passwordCopy = document.querySelector('#password-input');
let copyIcon = document.querySelector('#copy-icon'); // Select the copy icon
let passLen = document.getElementById('value');
let genHere = document.querySelector('#gen-here');
const strengthCircle = document.getElementById('circle-sign');
let password = "";
let uppercaseCheck = document.getElementById('uppercase-check');
let lowercaseCheck = document.getElementById('lowercase-check');
let numberCheck = document.getElementById('numbers-check');
let symbolCheck = document.getElementById('symbols-check');
let boolUpper = false;
let boolLower = false;
let boolNumber = false;
let boolSymbol = false;
const slider = document.getElementById("mySlider");
let len = slider.value;
slider.oninput = function() {
passLen.textContent = this.value;
len = this.value;
}
function checkCheckbox() {
boolUpper = uppercaseCheck.checked;
boolLower = lowercaseCheck.checked;
boolNumber = numberCheck.checked;
boolSymbol = symbolCheck.checked;
}
function changeCircleColor() {
let strength = 0;
// Check password conditions for strength
if (password.length >= 6) strength++; // Length greater than 6
if (/[A-Z]/.test(password)) strength++; // Contains uppercase letters
if (/[0-9]/.test(password)) strength++; // Contains numbers
if (/[\W]/.test(password)) strength++; // Contains symbols
// Reset circle's color
strengthCircle.classList.remove('weak', 'medium', 'strong');
// Apply different color based on strength
if (strength === 0 || strength === 1) {
strengthCircle.classList.add('weak'); // Weak strength
} else if (strength === 2) {
strengthCircle.classList.add('medium'); // Medium strength
} else if (strength >= 3) {
strengthCircle.classList.add('strong'); // Strong strength
}
}
function getPassword() {
let characterPool = "";
if (boolUpper) {
characterPool += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
}
if (boolLower) {
characterPool += "abcdefghijklmnopqrstuvwxyz";
}
if (boolNumber) {
characterPool += "0123456789";
}
if (boolSymbol) {
characterPool += "!@#$%^&*()_+[]{}|;:',.<>/?";
}
if (characterPool === "") {
alert('Please select at least one of the checkbox');
}
let password1 = "";
for (let i = 0; i < len; i++) {
let randomIndex = Math.floor(Math.random() * characterPool.length);
password1 += characterPool.charAt(randomIndex);
}
password = password1;
passwordCopy.value = password1;
changeCircleColor();
}
function genPassword() {
checkCheckbox();
getPassword();
}
genHere.addEventListener('click', genPassword);
// Copy password to clipboard functionality
copyIcon.addEventListener('click', function() {
if (passwordCopy.value) {
navigator.clipboard.writeText(passwordCopy.value).then(() => {
alert("Password copied to clipboard!");
}).catch(err => {
console.error("Failed to copy text: ", err);
});
} else {
alert("No password to copy!");
}
});