-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
62 lines (56 loc) · 2.17 KB
/
script.js
File metadata and controls
62 lines (56 loc) · 2.17 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
//Selectors
const inputEl = document.querySelector("#input");
const copyImgEl = document.querySelector(".fa-copy");
const password_length = document.querySelector(".password_length");
const uppercase_checkbox = document.querySelector(".uppercase_checkbox");
const lowercase_checkbox = document.querySelector(".lowercase_checkbox");
const number_checkbox = document.querySelector(".number_checkbox");
const symbols_checkbox = document.querySelector(".symbols_checkbox");
const generateBtn = document.querySelector("#btn");
const upperCaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lowerCaseChars = "abcdefghijklmnopqrstuvwxyz";
const numbers = "0123456789";
const symbols = "~!@#$%^&*()_+/;";
const getRandomData = (dataSet) => { // this funftion return 1 random data by using dataSet
return dataSet[Math.floor(Math.random() * dataSet.length)];
}
const generatePassword = (password = "") => { // this funftion generate random password
if(uppercase_checkbox.checked) {
password += getRandomData(upperCaseChars);
}
if(lowercase_checkbox.checked) {
password += getRandomData(lowerCaseChars);
}
if(number_checkbox.checked) {
password += getRandomData(numbers);
}
if(symbols_checkbox.checked) {
password += getRandomData(symbols);
}
if(password == "") {
alert("Please click one checkbox");
} else {
if(password.length < password_length.value) {
return generatePassword(password);
}
}
inputEl.value = truncateString(password, password_length.value);
}
generateBtn.addEventListener("click", ()=> { // this funftion call generatePassword when generate btn is on click
generatePassword();
if(inputEl.value) {
copyImgEl.classList.remove("hide");
}
});
copyImgEl.addEventListener("click", ()=> { // this funftion copy password by clicking copy image
navigator.clipboard.writeText(inputEl.value);
alert(`${inputEl.value} Copied`);
});
function truncateString(str, num) {
if(str.length > num) {
let substr = str.substring(0, num);
return substr;
} else {
return str;
}
}