-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasswordGenrator.js
More file actions
41 lines (32 loc) · 1.48 KB
/
passwordGenrator.js
File metadata and controls
41 lines (32 loc) · 1.48 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
function genratePassword(length, includeLowerCase, includeUpperCase, includeNumbers, includeSymbols) {
const lowerCaseChars = 'abcdefghijklmnopqrstuvwxyz';
const upperCaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const numberChars = '0123456789';
const symbolsChars = '!@#$%^&*()_+~`|}{[]:;?><,./-=';
let allowedChars = '';
let password = '';
allowedChars += includeLowerCase ? lowerCaseChars : '';
allowedChars += includeUpperCase ? upperCaseChars : '';
allowedChars += includeNumbers ? numberChars : '';
allowedChars += includeSymbols ? symbolsChars : '';
if (length <= 0) {
return '(Password length must be at least 1)';
}
if (allowedChars.length === 0) {
return '(Select at least one character type)';
}
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * allowedChars.length);
password += allowedChars.charAt(randomIndex);
}
return password;
}
document.getElementById("generateBtn").addEventListener("click", function () {
const length = document.getElementById("length").value;
const lower = document.getElementById("lowercase").checked;
const upper = document.getElementById("uppercase").checked;
const numbers = document.getElementById("numbers").checked;
const symbols = document.getElementById("symbols").checked;
const password = genratePassword(length, lower, upper, numbers, symbols);
document.getElementById("result").value = password;
});