forked from surajondev/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
110 lines (97 loc) · 3.97 KB
/
script.js
File metadata and controls
110 lines (97 loc) · 3.97 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const result = document.querySelector('#result'),
copyBtn = document.querySelector('.result-copy'),
length = document.querySelector('#length'),
uppercase = document.querySelector('#uppercase'),
lowercase = document.querySelector('#lowercase'),
numbers = document.querySelector('#numbers'),
symbols = document.querySelector('#symbols'),
generate = document.querySelector('#generate'),
message = document.querySelector('.message');
generate.addEventListener('click', function(e) {
e.preventDefault();
let passwordLength = length.value;
if(passwordLength > 4 && passwordLength <= 25) {
let password = '';
for(let i = 0; i < passwordLength; i++) {
password += generateRandomCharacter();
}
result.value = password;
message.textContent = "";
}
else {
message.textContent = "Select a length between 5 and 25";
if(message.classList.contains('message-success')) {
message.classList.remove('message-success');
}
result.value = "";
hideMessage();
}
});
copyBtn.addEventListener('click', function() {
const textarea = document.createElement('textarea');
const password = result.value;
if(!password) { return; }
textarea.value = password;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
textarea.remove();
message.textContent = "Password copied to clipboard";
message.classList.add('message-success');
hideMessage();
});
function getRandomUppercase() {
// (65 to 90) ascii values for (A to Z)
return String.fromCharCode(Math.floor(Math.random() * 26) + 65);
}
function getRandomLowercase() {
// (97 to 122) ascii values for (a to z)
return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
}
function getRandomNumber() {
return Math.floor(Math.random() * 10).toString();
}
function getRandomSymbol() {
const str = "!@#$%^&*()_+<>{}?:";
return str[Math.floor(Math.random() * str.length)];
}
// calling a random function from the above 4 functions
function generateRandomCharacter() {
if(uppercase.checked && lowercase.checked && numbers.checked && symbols.checked) {
const functions = {
0: getRandomUppercase,
1: getRandomLowercase,
2: getRandomNumber,
3: getRandomSymbol
}
return functions[Math.floor(Math.random() * 4)]();
}
if(uppercase.checked && lowercase.checked && numbers.checked && !symbols.checked) {
const functions = {
0: getRandomUppercase,
1: getRandomLowercase,
2: getRandomNumber
}
return functions[Math.floor(Math.random() * 3)]();
}
if(uppercase.checked && lowercase.checked && !numbers.checked && symbols.checked) {
const functions = {
0: getRandomUppercase,
1: getRandomLowercase,
2: getRandomSymbol
}
return functions[Math.floor(Math.random() * 3)]();
}
if(uppercase.checked && lowercase.checked && !numbers.checked && !symbols.checked) {
const functions = {
0: getRandomUppercase,
1: getRandomLowercase
}
return functions[Math.floor(Math.random() * 2)]();
}
}
function hideMessage() {
setTimeout(function() {
message.textContent = "";
}, 3000)
}