Answers for "javascript code for random password generator"

5

javascript password generator example

function generatePassword(passwordLength) {
  var numberChars = "0123456789";
  var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var lowerChars = "abcdefghijklmnopqrstuvwxyz";
  var allChars = numberChars + upperChars + lowerChars;
  var randPasswordArray = Array(passwordLength);
  randPasswordArray[0] = numberChars;
  randPasswordArray[1] = upperChars;
  randPasswordArray[2] = lowerChars;
  randPasswordArray = randPasswordArray.fill(allChars, 3);
  return shuffleArray(randPasswordArray.map(function(x) { return x[Math.floor(Math.random() * x.length)] })).join('');
}

function shuffleArray(array) {
  for (var i = array.length - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }
  return array;
}

alert(generatePassword(12));
Posted by: Guest on January-14-2020
0

Javascript random password generator Exampe

/**
 * Get Auto Generated Random Password
 * 
 * @param   int     length, eg: Default Length = 10
 * 
 * @return  string  Auto generated random password
 */
const getAutoGeneratedPassword = (length = 10) => {
    const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
          genPass = "";

    for (let i = 0, n = charset.length; i < length; ++i) {
        genPass += charset.charAt(Math.floor(Math.random() * n));
    }

    return genPass;
}
Posted by: Guest on August-21-2021

Code answers related to "javascript code for random password generator"

Code answers related to "Javascript"

Browse Popular Code Answers by Language