-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
67 lines (58 loc) · 2.35 KB
/
script.js
File metadata and controls
67 lines (58 loc) · 2.35 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
const passwordInput = document.getElementById("password");
const generateButton = document.getElementById("generate");
const copyButton = document.getElementById("copy");
function generatePassword() {
const length = Math.floor(Math.random() * (15 - 8 + 1)) + 8;
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=";
let password = "";
let hasLowercase = false;
let hasUppercase = false;
let hasNumberOrSymbol = false;
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * charset.length);
const randomChar = charset[randomIndex];
if (!hasLowercase && /[a-z]/.test(randomChar)) {
password += randomChar.toLowerCase();
hasLowercase = true;
} else if (!hasUppercase && /[A-Z]/.test(randomChar)) {
password += randomChar.toUpperCase();
hasUppercase = true;
} else if (!hasNumberOrSymbol && /[0-9!@#$%^&*()_+\-=]/.test(randomChar)) {
password += randomChar;
hasNumberOrSymbol = true;
} else {
password += randomChar;
}
}
if (!hasLowercase) {
// if there is no lowercase letter, replace a random character with a lowercase letter
const index = Math.floor(Math.random() * length);
password = password.slice(0, index) + "a" + password.slice(index + 1);
hasLowercase = true;
}
if (!hasUppercase) {
// if there is no uppercase letter, replace a random character with an uppercase letter
const index = Math.floor(Math.random() * length);
password = password.slice(0, index) + "A" + password.slice(index + 1);
hasUppercase = true;
}
if (!hasNumberOrSymbol) {
// if there is no number or symbol, replace a random character with a number or symbol
const index = Math.floor(Math.random() * length);
password = password.slice(0, index) + "0" + password.slice(index + 1);
hasNumberOrSymbol = true;
}
return password;
}
function copyPassword() {
passwordInput.select();
document.execCommand("copy");
}
generateButton.addEventListener("click", () => {
const password = generatePassword();
passwordInput.value = password;
copyButton.classList.remove("hidden");
});
copyButton.addEventListener("click", () => {
copyPassword();
});