-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (46 loc) · 1.76 KB
/
script.js
File metadata and controls
55 lines (46 loc) · 1.76 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
const passwordEl = document.getElementById('password');
const copyBtn = document.getElementById('copy-btn');
const generateBtn = document.getElementById('generate-btn');
const lengthInput = document.getElementById('length');
const uppercaseEl = document.getElementById('uppercase');
const lowercaseEl = document.getElementById('lowercase');
const numbersEl = document.getElementById('numbers');
const symbolsEl = document.getElementById('symbols');
const uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lowercaseChars = "abcdefghijklmnopqrstuvwxyz";
const numberChars = "0123456789";
const symbolChars = "!@#$%^&*";
function generatePassword() {
const length = parseInt(lengthInput.value);
const includeUppercase = uppercaseEl.checked;
const includeLowercase = lowercaseEl.checked;
const includeNumbers = numbersEl.checked;
const includeSymbols = symbolsEl.checked;
let allowedChars = "";
let password = "";
if (includeUppercase) allowedChars += uppercaseChars;
if (includeLowercase) allowedChars += lowercaseChars;
if (includeNumbers) allowedChars += numberChars;
if (includeSymbols) allowedChars += symbolChars;
if (allowedChars.length === 0) {
alert("Please select at least one character type!");
return;
}
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * allowedChars.length);
password += allowedChars[randomIndex];
}
passwordEl.value = password;
}
function copyToClipboard() {
const password = passwordEl.value;
if (!password) {
alert("No password to copy!");
return;
}
navigator.clipboard.writeText(password).then(() => {
alert("Password copied to clipboard!");
});
}
generateBtn.addEventListener('click', generatePassword);
copyBtn.addEventListener('click', copyToClipboard);