-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
52 lines (43 loc) · 1.49 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
const empty = "",
uCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
lCase = "abcdefghijklmnopqrstuvwxyz",
number = "0123456789",
symbol = "!@#$%^&*=-_";
const pLength = document.getElementById("p-length"),
upperCase = document.getElementById("p-uppercase"),
lowerCase = document.getElementById("p-lowercase"),
pNumber = document.getElementById("p-number"),
pSymbol = document.getElementById("p-symbol"),
submit = document.getElementById("submit"),
password = document.getElementById("password"),
copy = document.getElementById("copy");
submit.addEventListener("click", () => {
let initialPassword = empty;
//ADD CHARACTER IF AN OPTION IS CHECKED
upperCase.checked ? (initialPassword += uCase) : "";
lowerCase.checked ? (initialPassword += lCase) : "";
pNumber.checked ? (initialPassword += number) : "";
pSymbol.checked ? (initialPassword += symbol) : "";
password.value = generatePassword(pLength.value, initialPassword)
})
function generatePassword(l, initialPassword) {
let pass = "";
for (let i = 0; i < l; i++) {
pass += initialPassword.charAt(
Math.floor(Math.random() * initialPassword.length)
)
}
return pass;
}
// let str = "Hello";
// let result = str.charAt(0); // H
//Copy function
copy.addEventListener("click", () => {
if (password.value == "") {
alert("Please generate a password")
}else{
password.select();
document.execCommand("copy")
alert("Password has been copied to clipboard 🌟");
}
})