Skip to content

Commit 50472f8

Browse files
committed
Upload Project
1 parent b619561 commit 50472f8

File tree

5 files changed

+254
-2
lines changed

5 files changed

+254
-2
lines changed

README.md

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,90 @@
1-
# js-password-generator
2-
it enables you to generate your own passwords with different degrees of power and security and adjust them to the length, characters, etc. You can include it in your website very easily, where no outside libraries have been used. Only Java Script.
1+
# JS-Password-Generator :lock_with_ink_pen:
2+
3+
![image](assets/screenshot.jpg)
4+
5+
It enables you to generate your own passwords with different degrees of power and security and adjust them to the length, characters, etc. You can include it in your website very easily, where no outside libraries have been used. Only Java Script.
6+
7+
This writing method is not the only one and you can generate the same results in different ways in the same language.
8+
I hope it's useful to you in your work, and I'm going to constantly improve its coordination and add new features to it.
9+
10+
- Explain Code </br>
11+
Here we use some function to generate a strings from Uppercase and lowercase letters, numbers and symbols
12+
```java-script
13+
function getRandomLower() {
14+
return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
15+
}
16+
17+
function getRandomUpper() {
18+
return String.fromCharCode(Math.floor(Math.random() * 26) + 65);
19+
}
20+
21+
function getRandomNumber() {
22+
return String.fromCharCode(Math.floor(Math.random() * 10) + 48);
23+
}
24+
25+
function getRandomSymbol() {
26+
const symbols = "!@#$%^&*(){}[]=/<>,."
27+
return symbols[Math.floor(Math.random() * symbols.length)];
28+
}
29+
30+
const randomFunc = {
31+
lower: getRandomLower,
32+
upper: getRandomUpper,
33+
number: getRandomNumber,
34+
symbol: getRandomSymbol,
35+
};
36+
```
37+
This is the code that executes the command when the button is pressed in order to collect and generate those values
38+
```java-script
39+
const generate = document.getElementById("generateBtn");
40+
generate.addEventListener("click", () => {
41+
const length = document.getElementById("PasswordLength").value;
42+
const hasUpper = document.getElementById("uppercase").checked;
43+
const hasLower = document.getElementById("lowercase").checked;
44+
const hasNumber = document.getElementById("numbers").checked;
45+
const hasSymbol = document.getElementById("symbols").checked;
46+
const result = document.getElementById("PasswordResult");
47+
result.innerText = generatePassword(
48+
hasLower,
49+
hasUpper,
50+
hasNumber,
51+
hasSymbol,
52+
length
53+
);
54+
});
55+
56+
function generatePassword(lower, upper, number, symbol, length){
57+
let generatedPassword = "";
58+
const typesCount = lower + upper + number + symbol;
59+
const typesArr = [{lower}, {upper}, {number}, {symbol}].filter(
60+
(item) => Object.values(item)[0]
61+
);
62+
for (let i = 0; i < length; i += typesCount){
63+
typesArr.forEach((type) => {
64+
const funcName = Object.keys(type)[0];
65+
generatedPassword += randomFunc[funcName]();
66+
});
67+
}
68+
const finalPassword = generatedPassword.slice(0, length);
69+
return finalPassword;
70+
}
71+
```
72+
And the last ting is the code for the "copy to clipboard" button in order to copy the generated password.
73+
74+
```java-script
75+
let button = document.getElementById("clipboardBtn");
76+
button.addEventListener("click", (e) => {
77+
e.preventDefault();
78+
document.execCommand(
79+
"copy",
80+
false,
81+
document.getElementById("PasswordResult").select()
82+
);
83+
});
84+
```
85+
Note: This is optional, you can not add it, it's up to how useful this code can be in your projects.
86+
87+
Credits
88+
-------
89+
- [Linkedin Account](https://www.somar-kesen.com/) </br>
90+
- [My website](https://www.somar-kesen.com/)

assets/js.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
function getRandomLower() {
2+
return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
3+
}
4+
5+
function getRandomUpper() {
6+
return String.fromCharCode(Math.floor(Math.random() * 26) + 65);
7+
}
8+
9+
function getRandomNumber() {
10+
return String.fromCharCode(Math.floor(Math.random() * 10) + 48);
11+
}
12+
13+
function getRandomSymbol() {
14+
const symbols = "!@#$%^&*(){}[]=/<>,."
15+
return symbols[Math.floor(Math.random() * symbols.length)];
16+
}
17+
18+
const randomFunc = {
19+
lower: getRandomLower,
20+
upper: getRandomUpper,
21+
number: getRandomNumber,
22+
symbol: getRandomSymbol,
23+
};
24+
25+
const generate = document.getElementById("generateBtn");
26+
generate.addEventListener("click", () => {
27+
const length = document.getElementById("PasswordLength").value;
28+
const hasUpper = document.getElementById("uppercase").checked;
29+
const hasLower = document.getElementById("lowercase").checked;
30+
const hasNumber = document.getElementById("numbers").checked;
31+
const hasSymbol = document.getElementById("symbols").checked;
32+
const result = document.getElementById("PasswordResult");
33+
result.innerText = generatePassword(
34+
hasLower,
35+
hasUpper,
36+
hasNumber,
37+
hasSymbol,
38+
length
39+
);
40+
});
41+
42+
function generatePassword(lower, upper, number, symbol, length){
43+
let generatedPassword = "";
44+
const typesCount = lower + upper + number + symbol;
45+
const typesArr = [{lower}, {upper}, {number}, {symbol}].filter(
46+
(item) => Object.values(item)[0]
47+
);
48+
for (let i = 0; i < length; i += typesCount){
49+
typesArr.forEach((type) => {
50+
const funcName = Object.keys(type)[0];
51+
generatedPassword += randomFunc[funcName]();
52+
});
53+
}
54+
const finalPassword = generatedPassword.slice(0, length);
55+
return finalPassword;
56+
}
57+
58+
let button = document.getElementById("clipboardBtn");
59+
button.addEventListener("click", (e) => {
60+
e.preventDefault();
61+
document.execCommand(
62+
"copy",
63+
false,
64+
document.getElementById("PasswordResult").select()
65+
);
66+
});

assets/screenshot.jpg

50.4 KB
Loading

assets/style.css

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
.result-container{
2+
background-color: var(--bg-color);
3+
border: 12px solid var(--bg-color);
4+
filter: var(--filter2);
5+
display: flex;
6+
justify-content: flex-start;
7+
align-items: center;
8+
position: relative;
9+
font-size: 18px;
10+
letter-spacing: 1px;
11+
height: 60px;
12+
width: 100%;
13+
margin-bottom: 35px;
14+
}
15+
16+
.result-container .btn{
17+
font-size: 20px;
18+
position: absolute;
19+
top: 5px;
20+
right: 5px;
21+
height: 40px;
22+
width: 40px;
23+
}
24+
25+
textarea{
26+
background: none;
27+
border: solid 3px;
28+
height : 42px;
29+
color: var(--color);
30+
font-size: 20px;
31+
margin-top: auto;
32+
outline: none;
33+
}
34+
35+
.buttons{
36+
display: flex;
37+
}
38+
39+
.btn{
40+
width: 50%;
41+
border: solid;
42+
color: var(--color);
43+
cursor: pointer;
44+
font-size: 16px;
45+
padding: 8px 12px;
46+
margin: 14px 5px 7px 5px;
47+
background-color: var(--bg-color);
48+
filter: var(--filter2);
49+
}

index.html

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
8+
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
9+
<link rel="stylesheet" href="assets/style.css">
10+
<title>JS Password Generator</title>
11+
</head>
12+
<body>
13+
<div class="result-container">
14+
<textarea name="" id="PasswordResult" cols="30" rows="10" placeholder="Your Password will show Here"></textarea>
15+
</div>
16+
<div class="setting">
17+
<div class="setting">
18+
<label>Password Length</label>
19+
<input type="number" id="PasswordLength" min="4" max="20" value="8">
20+
</div>
21+
<div class="setting">
22+
<label>Include Uppercase Letters</label>
23+
<input type="checkbox" id="uppercase" checked>
24+
</div>
25+
<div class="setting">
26+
<label>Include Lowercase Letters</label>
27+
<input type="checkbox" id="lowercase" checked>
28+
</div>
29+
<div class="setting">
30+
<label>Include Numbers</label>
31+
<input type="checkbox" id="numbers" checked>
32+
</div>
33+
<div class="setting">
34+
<label>Include Symbols</label>
35+
<input type="checkbox" id="symbols" checked>
36+
</div>
37+
</div>
38+
<div class="buttouns">
39+
<button class="btn btn-large" id="generateBtn">
40+
<i class="fas fa-key"></i> Generate
41+
</button>
42+
<button class="btn" id="clipboardBtn">
43+
<i class="far fa-clipboard"></i> Copy
44+
</button>
45+
</div>
46+
<script src="assets/js.js"></script>
47+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
48+
</body>
49+
</html>

0 commit comments

Comments
 (0)