-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
64 lines (52 loc) · 2.26 KB
/
script.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
// RANDOM PASSWORD GENERATOR
function generatePassword(lenght, includeLowercase, includeUppercase, includeNumbers, includeSymbols){
const lowercaseChar = "abcdefghijklmnopqrstuvwxyz";
const uppercaseChar = "ABCDEFGHIJKLNNOPQRSTUVWXYZ";
const numberChar = "0123456789";
const symboleChar = "!@#$%^&*()_+-=: ><,.";
let allowedChar = "";
let password = "";
allowedChar += includeLowercase ? lowercaseChar : "";
allowedChar += includeUppercase ? uppercaseChar : "";
allowedChar += includeNumbers ? numberChar : "";
allowedChar += includeSymbols ? symboleChar : "";
if(lenght <= 6){
return `password lenght must be at least 7 character`;
}
if(allowedChar.length === 0){
return `At least one of set of character must be selected`;
}
for(let i = 0 ; i <= lenght ; i++){
const randomIndex = Math.floor(Math.random() * allowedChar.length);
password += allowedChar[randomIndex];
}
return password;
}
document.getElementById('generateButton').addEventListener('click', function() {
const passwordLenght = parseInt(document.getElementById('passwordLength').value - 1);
const includeLowercase = true;
const includeUppercase = true;
const includeNumbers = true;
const includeSymbols = true;
const password = generatePassword(passwordLenght,
includeLowercase,
includeUppercase,
includeNumbers,
includeSymbols);
console.log(`the password generated is : ${password} `);
document.getElementById('passwordDisplay').textContent = `The password generated is: ${password}`;
});
document.getElementById('copyButton').addEventListener('click', function() {
const passwordText = document.getElementById('passwordDisplay').textContent.split(": ")[1];
if (passwordText) {
const textarea = document.createElement('textarea');
textarea.value = passwordText;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
alert(`"${passwordText}" copied!`);
} else {
alert(`No password to copy!`);
}
});