-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_gen.js
45 lines (40 loc) · 1.55 KB
/
password_gen.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
const empty = "";
const ucase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lcase = "abcdefghijklmnopqrstuvwxyz";
const number = "0123456789";
const symbol = "!@#$%^&*~";
const plength = document.getElementById("p-lenght");
const upperCase = document.getElementById("p-uppercase");
const lowerCase = document.getElementById("p-lowercase");
const pNumber = document.getElementById("p-number");
const pSymbol = document.getElementById("p-symbol");
const submit = document.getElementById("submit");
const password = document.getElementById("password");
submit.addEventListener("click", () => {
let initialpassword = empty;
(upperCase.checked) ? initialpassword += ucase : "";
(lowerCase.checked) ? initialpassword += lcase : "";
(pNumber.checked) ? initialpassword += number : "";
(pSymbol.checked) ? initialpassword += symbol : "";
password.value = generatePassword(plength.value , initialpassword)
});
function generatePassword(l, initialpassword){
let pass = "";
for(let i = 0; i<l; i++){
pass += initialpassword.charAt(Math.floor(Math.random() *
initialpassword.length ));
}
return pass;
}
//Copying password to clipboard
const copy = document.getElementById("copy");
copy.addEventListener("click", () => {
if( password.value == ""){
alert("Please Generate password first");
}
else{
password.select();
document.execCommand("copy");
alert("Password copied successfully!");
}
})