Skip to content

Commit

Permalink
Merge pull request #147 from nilesh-fatfatwale/main
Browse files Browse the repository at this point in the history
Added Custom password Length and copy button
  • Loading branch information
iamrahulmahato authored Oct 2, 2024
2 parents 6e21fa3 + 3ea84ad commit ecdb8f9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
3 changes: 3 additions & 0 deletions projects/Password Generator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
<main>
<h1>Click to generate a random password!</h1>
<input type="text" id="password">
<input type="range" id="passwordLength" min="1" max="50" value="8" oninput="updateLengthDisplay()">
<label for="passwordLength">Password length: <span id="lengthDisplay">8</span></label>
<div>
<button id="create">Create</button>
<button id="copy">Copy</button>
<button id="clear">Clear</button>
</div>
</main>
Expand Down
41 changes: 23 additions & 18 deletions projects/Password Generator/script.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const field = document.querySelector('#password');
const create = document.querySelector('#create');
const clear = document.querySelector('#clear');

const copy = document.querySelector('#copy');
const passwordLength = document.querySelector('#passwordLength');
const lengthDisplay = document.querySelector('#lengthDisplay');

const uppercase = 'QWERTYUIOPASDFGHJKLZXCVBNM';
const numbers = '1234567890';
Expand All @@ -12,7 +14,7 @@ String.prototype.shuffle = function () {
var a = this.split(""),
n = a.length;

for(var i = n - 1; i > 0; i--) {
for (var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
Expand All @@ -21,27 +23,30 @@ String.prototype.shuffle = function () {
return a.join("");
}

function randomPassword(){
function randomPassword(length) {
let characters = uppercase + lowercase + numbers + symbols;
let password = '';
for (let i = 0; i < 3; i++){
password += uppercase.charAt(Math.floor(Math.random() * uppercase.length))
}
for (let i = 0; i < 3; i++){
password += lowercase.charAt(Math.floor(Math.random() * lowercase.length))
}
for (let i = 0; i < 2; i++){
password += numbers.charAt(Math.floor(Math.random() * numbers.length))
}
for (let i = 0; i < 2; i++){
password += symbols.charAt(Math.floor(Math.random() * symbols.length))

for (let i = 0; i < length; i++) {
password += characters.charAt(Math.floor(Math.random() * characters.length));
}

return password.shuffle();
}

clear.addEventListener('click', ()=>{
clear.addEventListener('click', () => {
field.value = '';
})

create.addEventListener('click', ()=>{
field.value = randomPassword();
})
create.addEventListener('click', () => {
const length = parseInt(passwordLength.value);
field.value = randomPassword(length);
})

copy.addEventListener('click', () => {
navigator.clipboard.writeText(field.value);
})

function updateLengthDisplay() {
lengthDisplay.textContent = passwordLength.value;
}

0 comments on commit ecdb8f9

Please sign in to comment.