简体   繁体   中英

How to make a password generator with javascript where i can toggle length, special characters, numbers, uppercase and lowercase

function genPassword() {
    var chars = "0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var passwordLength = prompt("Length:Enter a number between 8-128");
    var specialcharacters = prompt("Would you like special characters?");
    var numeric = prompt("Would you like numbers?");
    var upper = prompt("Would you like uppercase?");
    var lower = prompt("Would you like lowercase?");
    var password = "";
for (var i = 0; i <= passwordLength; i++) {
   var randomNumber = Math.floor(Math.random() * chars.length);
password += chars.substring(randomNumber, randomNumber +1);
  }
        document.getElementById("password").value = password;
}

When i click my generate btn i should get 5 prompts and the prompts should us that info to create the password like 8(for length), yes, no, no, yes.

  • Start with four different strings, one for each group of characters
  • Start with empty chars
  • For each "yes", append the appropriate character group to chars
  • Then pick at random from the constructed chars .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM