-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (74 loc) · 2.85 KB
/
index.js
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
let lengthofpassword = 10;
document.getElementById('simpleRange').addEventListener('input', function () {
document.getElementsByClassName('data-password-length')[0].textContent = this.value;
lengthofpassword = this.value;
});
function get_conditions() {
let selected_conditions=[];
for(let i=1;i<=4;i++){
let id_name= "o"+i;
selected_conditions[i-1]=document.getElementById(id_name).checked;
}
return selected_conditions;
}
const symbols = [
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'-', '_', '=', '+', '{', '}', '[', ']', '|', '\\',
':', ';', '"', "'", '<', '>', ',', '.', '/', '?'
];
console.log(symbols.length)
document.querySelector('.generate_password').addEventListener('click', function () {
lengthofpassword = document.getElementsByClassName('data-password-length ')[0].textContent;
let arr = get_conditions();
let count = 0;
for (let i = 0; i < 4; i++){
if (arr[i]) {
count++;
}
}
if (count == 0) {
alert('please select some required fields');
return;
}
if (count >= 2 || lengthofpassword >= 10) {
document.getElementsByClassName('image_show')[0].src = 'green.jpg'
}
else {
document.getElementsByClassName('image_show')[0].src = 'red.png';
}
console.log(arr);
let password = "";
let lengthgenerated = 0;
while (lengthgenerated < lengthofpassword) {
let index = Math.floor(Math.random() * 4);
if (arr[index]) {
lengthgenerated++;
if (index == 0) {
password += String.fromCharCode(65 + Math.floor(Math.random() * 26));
}
else if (index == 1) {
password += String.fromCharCode(97 + Math.floor(Math.random() * 26));
}
else if (index == 2) {
password += symbols[Math.floor(Math.random() * 30)];
}
else if (index == 3) {
password += Math.floor(Math.random() * 10);
}
}
}
document.getElementsByClassName('password_show')[0].value = password;
});
document.getElementById('copyButton').addEventListener('click', () => {
// Get the text from the input field
const textToCopy = document.getElementsByClassName('password_show')[0].value;
// Use the Clipboard API to copy the text
navigator.clipboard.writeText(textToCopy)
.then(() => {
// Optional: Provide feedback to the user
alert('Text copied to clipboard!');
})
.catch(err => {
console.error('Failed to copy text: ', err);
});
});