-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
89 lines (67 loc) · 2.74 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// DOM constants
const passwordEl = document.getElementById('password');
const lengthEl = document.getElementById('length');
const uppercaseEl = document.getElementById('uppercase');
const lowercaseEl = document.getElementById('lowercase');
const numbersEl = document.getElementById('numbers');
const symbolsEl = document.getElementById('symbols');
const btnGenerate = document.getElementById('generate-password');
const btnClipboard = document.getElementById('clipboard');
// Main password function
function createPassword () {
// Password array constants and accumulator
const upperArr = ["ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
const lowerArr = ["abcdefghijklmnopqrstuvwxyz"];
const numberArr = ["1234567890"];
const symbolArr = [" `~!@#$%^&*()-_=+{}[]:;'|<,>.?/"];
// Combined password variable
let combinedPass = "";
// Get password length
let passLength = parseInt(lengthEl.value);
if (passLength < 8 || passLength > 128 || passLength === null) {
alert("Password must contain at least 8 characters and be no longer than 128 characters. Please check your number and try again.");
return "";
}
// Build combined password array
if ((uppercaseEl.checked || lowercaseEl.checked || numbersEl.checked || symbolsEl.checked) === false) {
alert("You must choose at least one checkbox to build a password.");
return "";
}
if (uppercaseEl.checked === true) {
combinedPass += upperArr;
}
if (lowercaseEl.checked === true) {
combinedPass += lowerArr;
}
if (numbersEl.checked === true) {
combinedPass += numberArr;
}
if (symbolsEl.checked === true) {
combinedPass += symbolArr;
}
// Build password
let generatedPassword = "";
for (var i = 0; i < passLength; i++ ) {
generatedPassword += combinedPass[Math.floor(Math.random() * combinedPass.length)];
}
// Display completed password in text field
passwordEl.innerText = generatedPassword;
}
// Capture click event from Generate Password button to create password
btnGenerate.addEventListener("click", createPassword);
// Capture click event from Clipboard button to copy password to clipboard
btnClipboard.addEventListener("click", function() {
// Create temporary text area and set value to current password field
const textarea = document.createElement('textarea');
const pswd = passwordEl.innerText;
if (!pswd) {
return;
}
// Add temporary text area to body, select password text, copy to clipboard, then remove text area
textarea.value = pswd;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
textarea.remove();
alert("Password copied to the clipboard!");
});