-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (61 loc) · 2 KB
/
script.js
File metadata and controls
71 lines (61 loc) · 2 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
const upperCase = "ABCDEFCHIJKLMNOPQRSTUVWXYZ";
const lowerCase = "abcdefghijklmnopqrstuvwxyz";
const number = "1234567890";
const symbols = "~!@#$%&*_|\?/.";
const passBox = document.getElementById('pass-box');
const totalChar = document.getElementById('total-char');
const upperInput = document.getElementById('upper-case');
const lowerInput = document.getElementById('lower-case');
const numbersInput = document.getElementById('numbers');
const symbolsInput = document.getElementById('symbols');
const getRandomData = (dataSet) => {
return dataSet[
Math.floor(
Math.random() * dataSet.length
)
]
}
const generatePassword = () => {
let password = "";
const length = parseInt(totalChar.value);
const selectedCharacterSets = [];
if (upperInput.checked) {
selectedCharacterSets.push(upperCase);
}
if (lowerInput.checked) {
selectedCharacterSets.push(lowerCase);
}
if (numbersInput.checked) {
selectedCharacterSets.push(number);
}
if (symbolsInput.checked) {
selectedCharacterSets.push(symbols);
}
if (selectedCharacterSets.length === 0) {
console.log("Please select at least one character set.");
return;
}
while (password.length < length) {
const randomSetIndex = Math.floor(Math.random() * selectedCharacterSets.length);
const randomSet = selectedCharacterSets[randomSetIndex];
password += getRandomData(randomSet);
}
//truncate kiye yaha
const truncatedPassword = truncateString(password, length);
console.log(truncatedPassword);
passBox.innerText = truncatedPassword;
};
generatePassword();
document.getElementById('btn').addEventListener(
'click',
function() {
generatePassword();
}
);
//truncate kane ka function
function truncateString(str, num){
if(str.length > num){
let subStr = str.substring(0, num);
return subStr;
}else return str;
}