|
| 1 | +const Generate = document.getElementById("generate"); |
| 2 | +const text = document.getElementById("Password"); |
| 3 | +const copy = document.getElementById("copy"); |
| 4 | + |
| 5 | +let passwordLength |
| 6 | +let includeLowercase |
| 7 | +let includeUppercase |
| 8 | +let includeNumbers |
| 9 | +let includeSymbols |
| 10 | + |
| 11 | + |
| 12 | +let allowedChars = "" |
| 13 | +let generatedPassword = "" |
| 14 | + |
| 15 | + |
| 16 | +const lowercaseChars = "abcdefghijklmnopqrstuvwxyz"; |
| 17 | +const uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 18 | +const numberChars = "0123456789"; |
| 19 | +const symbolChars = "!@#$%^&*()_+-="; |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +Generate.onclick = function(){ |
| 24 | + |
| 25 | + passwordLength = document.getElementById("Length").value; |
| 26 | + includeLowercase = document.getElementById("lower"); |
| 27 | + includeUppercase = document.getElementById("upper"); |
| 28 | + includeNumbers = document.getElementById("numb"); |
| 29 | + includeSymbols = document.getElementById("symb"); |
| 30 | + |
| 31 | + |
| 32 | + function password(passwordLength, includeLowercase, includeUppercase, includeNumbers, includeSymbols){ |
| 33 | + |
| 34 | + |
| 35 | + allowedChars += includeLowercase.checked ? lowercaseChars : ""; |
| 36 | + allowedChars += includeUppercase.checked ? uppercaseChars : ""; |
| 37 | + allowedChars += includeNumbers.checked ? numberChars : ""; |
| 38 | + allowedChars += includeSymbols.checked ? symbolChars : ""; |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + if(passwordLength <= 0 || passwordLength==null){ |
| 43 | + return `(Password length must be at least 1)`; |
| 44 | + } |
| 45 | + if(allowedChars.length === 0){ |
| 46 | + return `(At least 1 set of character needs to be selected)`; |
| 47 | + } |
| 48 | + |
| 49 | + for(let i = 0; i < passwordLength; i++){ |
| 50 | + const randomIndex = Math.floor(Math.random() * allowedChars.length); |
| 51 | + generatedPassword += allowedChars[randomIndex]; |
| 52 | + } |
| 53 | + |
| 54 | + return generatedPassword; |
| 55 | + } |
| 56 | + |
| 57 | + const Password = password(passwordLength, includeLowercase, includeUppercase, includeNumbers, includeSymbols); |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + text.textContent = Password; |
| 62 | + console.log(Password); |
| 63 | + generatedPassword = ""; |
| 64 | + allowedChars = ""; |
| 65 | +} |
| 66 | + |
| 67 | +copy.onclick = function(){ |
| 68 | + navigator.clipboard.writeText(text.textContent); |
| 69 | +} |
| 70 | + |
0 commit comments