-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
86 lines (71 loc) · 2.61 KB
/
script.js
File metadata and controls
86 lines (71 loc) · 2.61 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
const numberOfCharacters = document.getElementById("NumberofCharacters");
const uppercaseCharacters = document.getElementById("uppercase");
const lowercaseCharacters = document.getElementById("lowercase");
const numbers = document.getElementById("numbers");
const symbols = document.getElementById("symbols");
const button = document.getElementById("submit");
const userInput = document.getElementById("Uname");
const passwordOutput = document.getElementById("password");
const clipboard = document.getElementById("fclipboard");
let mainString = "";
let count = 0;
function initialise(low, high) {
let charArray = "";
for (let index = low; index <= high; index++) {
charArray += String.fromCharCode(index);
}
return charArray;
}
let upcharacters = initialise(65, 90);
let lccharacters = initialise(97, 122);
let numcharacters = initialise(48, 57);
let symcharacters = initialise(33, 47);
button.addEventListener("click", () => {
if (uppercaseCharacters.checked) {
mainString += upcharacters;
count++;
}
if (lowercaseCharacters.checked) {
mainString += lccharacters;
count++;
}
if (numbers.checked) {
mainString += numcharacters;
count++;
}
if (symbols.checked) {
mainString += symcharacters;
count++;
}
generatePassword();
});
function generatePassword() {
if (numberOfCharacters.value >= count + userInput.value.length) {
let password = "";
if (userInput.value != null) password += userInput.value;
if (uppercaseCharacters.checked) password += upcharacters.charAt(Math.floor(Math.random()*27));
if (lowercaseCharacters.checked) password += lccharacters.charAt(Math.floor(Math.random()*27));
if (numbers.checked) password += numcharacters.charAt(Math.floor(Math.random()*numcharacters.length));
if (symbols.checked) password += symcharacters.charAt(Math.floor(Math.random()*symcharacters.length));
let size = password.length;
while (size != numberOfCharacters.value) {
password += mainString.charAt(Math.floor(Math.random()*mainString.length));
size = size + 1;
}
passwordOutput.value = password;
mainString = "";
count = 0;
}else{
alert("Please increase the number of characters limit");
count = 0;
}
}
clipboard.addEventListener("click",()=>{
if(passwordOutput.value != null){
passwordOutput.select();
document.execCommand("copy");
alert("Copied the text: " + passwordOutput.value);
}else{
alert("Please generate a password to copy");
}
});