-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
170 lines (139 loc) · 5.17 KB
/
index.html
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Password Generator</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;700&display=swap" rel="stylesheet">
</head>
<body>
<div class="main">
<label for="title" class="title">Password Generator</label>
<input type="text" aria-label="password input" id="passwordOutput" readonly>
<div class="Lbl-chek">
<label class="labelFor">Length of password:</label>
<input id="passwordLength" type="number" placeholder="0">
</div>
<div class="Lbl-chek">
<label class="labelFor" for="includeUpperCase">Include upper case letters:</label>
<input id="includeUpperCase" class="checkbox" type="checkbox" aria-label="checkbox" name="includeUpperCase">
</div>
<div class="Lbl-chek">
<label class="labelFor" for="includeNumbers">Include numbers:</label>
<input id="includeNumbers" class="checkbox" type="checkbox" aria-label="checkbox" name="includeNumbers">
</div>
<div class="Lbl-chek">
<label class="labelFor" for="includeSpecialChars">Include special characters:</label>
<input id="includeSpecialChars" class="checkbox" type="checkbox" aria-label="checkbox" name="includeSpecialChars">
</div>
<button id="generateButton">Generate</button>
</div>
<style>
* {
margin: 0;
padding: 0;
font-family: 'Poppins';
}
.main {
margin: 100px auto;
display: flex;
flex-direction: column;
width: 100%;
max-width: 500px;
justify-content: center;
align-items: center;
border: solid 1px black;
padding: 20px;
gap: 10px;
border-radius: 10px;
}
.title {
font-size: 30px;
font-weight: 600;
}
#passwordOutput {
width: 50%;
border: solid 1px black;
height: 30px;
padding: 3px;
text-align: center;
}
#passwordLength {
padding: 5px;
width: 50px;
text-align: center;
}
.Lbl-chek {
width: 100%;
display: flex;
justify-content: space-between;
}
.checkbox {
width: 50px;
}
#generateButton {
padding: 10px;
border-radius: 10px;
border: solid 1px black;
background-color: white;
margin: 10px;
color: black;
transition: all .3s;
}
#generateButton:hover {
background-color: black;
color: white;
}
</style>
<script>
// Get HTML elements by their IDs
const passwordOutput = document.getElementById('passwordOutput');
const passwordLength = document.getElementById('passwordLength');
const includeNumbers = document.getElementById('includeNumbers');
const includeSpecialChars = document.getElementById('includeSpecialChars');
const includeUpperCase = document.getElementById('includeUpperCase');
const generateButton = document.getElementById('generateButton');
// Arrays of characters to include in the password
const numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const lowerLetters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
const upperLetters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
const specialChars = ['!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~'];
// Function to generate password
function generatePassword() {
let availableChars = lowerLetters.slice(); // Start with lower case letters
if(passwordLength.value > 0 ){
const passwordLen = parseInt(passwordLength.value);
passwordOutput.style.color = 'black';
const isUpperCaseIncluded = includeUpperCase.checked;
const isNumbersIncluded = includeNumbers.checked;
const isSpecialCharsIncluded = includeSpecialChars.checked;
// Add characters based on selected options
if (isUpperCaseIncluded) {
availableChars = availableChars.concat(upperLetters);
}
if (isNumbersIncluded) {
availableChars = availableChars.concat(numbers);
}
if (isSpecialCharsIncluded) {
availableChars = availableChars.concat(specialChars);
}
// Shuffle the array of available characters
for (let i = availableChars.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[availableChars[i], availableChars[j]] = [availableChars[j], availableChars[i]];
}
// Create the password by slicing the shuffled array
const password = availableChars.slice(0, passwordLen).join('');
passwordOutput.value = password;
}
else{
passwordOutput.style.color = 'red';
passwordOutput.value = 'Choose a number greater than 0';
}
}
// Add event listener to generate button
generateButton.addEventListener('click', generatePassword);
</script>
</body>
</html>