Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
52 lines (40 loc) · 1.59 KB
/
Copy pathscript.js
File metadata and controls
52 lines (40 loc) · 1.59 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
const lengthSlider = document.getElementById("length");
const lengthValue = document.getElementById("lengthValue");
const passwordOutput = document.getElementById("passwordOutput");
const generateBtn = document.getElementById("generateBtn");
const copyBtn = document.getElementById("copyBtn");
const uppercaseCheck = document.getElementById("uppercase");
const numbersCheck = document.getElementById("numbers");
const symbolsCheck = document.getElementById("symbols");
const UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
const NUMBERS = "0123456789";
const SYMBOLS = "!@#$%^&*()_+-=[]{}|;:,.<>?";
lengthSlider.addEventListener("input", () => {
lengthValue.textContent = lengthSlider.value;
});
generateBtn.addEventListener("click", generatePassword);
copyBtn.addEventListener("click", copyPassword);
function generatePassword() {
const length = parseInt(lengthSlider.value);
let charPool = LOWERCASE;
if (uppercaseCheck.checked) charPool += UPPERCASE;
if (numbersCheck.checked) charPool += NUMBERS;
if (symbolsCheck.checked) charPool += SYMBOLS;
let password = "";
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * charPool.length);
password += charPool[randomIndex];
}
passwordOutput.value = password;
}
function copyPassword() {
if (!passwordOutput.value) return;
passwordOutput.select();
navigator.clipboard.writeText(passwordOutput.value);
copyBtn.textContent = "Copied!";
setTimeout(() => {
copyBtn.textContent = "Copy";
}, 1500);
}
generatePassword();