-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword.js
59 lines (48 loc) · 1.83 KB
/
password.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
let passwordLength = 8;
let isUpperCase = false;
let isNumbers = false;
let isSymbols = false;
const passwordRangeInputEl = document.getElementById("passRangeInput");
const passRangeValueEl = document.getElementById("passRangeValue");
const genBtn = document.getElementById("genBtn");
const passwordEl = document.getElementById("password");
const generatePassword = (passLength) => {
const lowerCaseLetters = "abcdefghijklmnopqrstuvwxyz";
const upperCaseLetters = isUpperCase ? "ABCDEFGHIJKLMNOPQRSTUVWXYZ" : "";
const numbers = isNumbers ? "0123456789" : "";
const symbols = isSymbols ? "!@#$%^&*()_+" : "";
const passwordChar = lowerCaseLetters + upperCaseLetters + numbers + symbols;
let password = "";
for (let i = 0; i < passLength; i++) {
const charIndex = Math.floor(Math.random() * passwordChar.length);
password += passwordChar[charIndex];
}
return password;
};
passwordRangeInputEl.addEventListener("input", (e) => {
passwordLength = +e.target.value;
passRangeValueEl.innerText = passwordLength;
});
genBtn.addEventListener("click", () => {
const upperCaseCheckEl = document.getElementById("uppercase");
const numbersCheckEl = document.getElementById("numbers");
const symbolsCheckEl = document.getElementById("symbols");
isUpperCase = upperCaseCheckEl.checked;
isNumbers = numbersCheckEl.checked;
isSymbols = symbolsCheckEl.checked;
const password = generatePassword(passwordLength);
passwordEl.innerHTML = password;
console.log("password", password);
});
passwordEl.addEventListener("click", (e) => {
if (e.target.innerText.length > 0) {
navigator.clipboard
.writeText(passwordEl.innerText)
.then(() => {
alert("Copied to clipboard");
})
.catch((err) => {
alert("could not copy");
});
}
});