Passwords generator is a web application that generates a password between 8 and 128 characters based on certain requirements chosen by the user. It also has an option for copying the password to the clipboard.
This application is compatible with the most commonly used web browsers
generatePassword();
- This function calls on checkRequirements() to set values to the requirements booleans. It will be false if the user cancels the prompt. Based on the requirements it pushes the desired characters into a pool choices. It generates the password by randomly chosing a character from the choices pool and addidng it to the password. This is reapeated according to the chosen length of the password. Then, the password is displayed and variables are cleared for future use.
function generatePassword() {
let check = checkRequirements();
if (check) {
// Add to pool of char choices
if (options.hasLowerCase) {
choices += lowerCaseChars;
} if (options.hasUpperCase) {
choices += upperCaseChars;
} if (options.hasNumericChars) {
choices += numericChars;
} if (options.hasSpecialChars) {
choices += specialChars;
}
for (var i = 0; i < options.length; i++) {
var char = choices[Math.floor(Math.random() * choices.length)];
newPassword += char;
}
//display password
passwordEl.innerHTML = newPassword;
clear();
}
}
copyToClipboard();
- This shows the function copyToClipboard(). This function creates an input element genPassword wit the password value. This element then is used to select the password value and execute a copy command on it. This effectively copies the password to the clipboard.
function copyToClipboard() {
var genPass = document.createElement('input');
document.body.appendChild(genPass);
genPass.value = passwordPointer.innerText;
genPass.select();
genPass.setSelectionRange(0, 99999);
document.execCommand("copy", false);
genPass.remove();
alert("Password copied to clipboard!");
}
- Carlos Toledo: portfolio
- Github: kqarlos
- LinkedIn: carlos-toledo415