-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
72 lines (62 loc) · 2.27 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Lara Nicole - Updated 2025
// Query selectors for the inputs
const generateBtn = document.querySelector("#generate");
const copyBtn = document.querySelector("#copy");
const passwordText = document.querySelector("#password");
const lengthInput = document.querySelector("#length");
const lowercaseInput = document.querySelector("#lowercase");
const uppercaseInput = document.querySelector("#uppercase");
const numbersInput = document.querySelector("#numbers");
const symbolsInput = document.querySelector("#symbols");
// Characters sets
const lower = "abcdefghijklmnopqrstuvwxyz";
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const number = "0123456789";
const symbol = "~!@#$%^&*()_+{}:?><;.,";
// Generate Password function
function generatePassword() {
const length = parseInt(lengthInput.value, 10);
const includeLowercase = lowercaseInput.checked;
const includeUppercase = uppercaseInput.checked;
const includeNumbers = numbersInput.checked;
const includeSymbols = symbolsInput.checked;
// Validate inputs
if (isNaN(length) || length < 8 || length > 128) {
alert("Password length must be a number between 8 and 128.");
return '';
}
if (!includeLowercase && !includeUppercase && !includeNumbers && !includeSymbols) {
alert("You must select at least one character type.");
return '';
}
// Build the usable character set
let usableCharacters = "";
if (includeLowercase) usableCharacters += lower;
if (includeUppercase) usableCharacters += upper;
if (includeNumbers) usableCharacters += number;
if (includeSymbols) usableCharacters += symbol;
// Generate the password
let password = "";
for (let i = 0; i < length; i++) {
password += usableCharacters[Math.floor(Math.random() * usableCharacters.length)];
}
return password;
}
// Write password to the #password input
function writePassword() {
const password = generatePassword();
passwordText.value = password;
}
// Copy password to clipboard
function copyToClipboard() {
if (passwordText.value) {
navigator.clipboard.writeText(passwordText.value).then(() => {
alert("Password copied to clipboard!");
});
} else {
alert("No password to copy!");
}
}
// Event listeners
generateBtn.addEventListener("click", writePassword);
copyBtn.addEventListener("click", copyToClipboard);