-3

I use this script to generate password on zapier. It works well but I found it sometimes generates password without number. Can you give me live demo of it here

function generateP() {
  var pass = '';
  var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '1234567890' + '!@#$%^&()_+~`|}{[]:;?><,./-=';
  for (var i = 1; i <= 16; i++) {
    var char = Math.floor(Math.random() * str.length + 1);
    pass += str.charAt(char)
  }
  return pass;
}
console.log(generateP())
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Aab B
  • 75
  • 6
  • So you want to generate passwords with EVERY type of symbol? – Yanb Aug 26 '21 at 08:38
  • 1
    Take one random upper case letter, one random lower case letter, one random digit, one random symbol and 12 random characters from all groups. Shuffle them. – jabaa Aug 26 '21 at 08:39
  • This script work well but it sometimes generates password without numbers – Aab B Aug 26 '21 at 08:42
  • I need to solve it – Aab B Aug 26 '21 at 08:42
  • This will help: [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – jabaa Aug 26 '21 at 08:44

2 Answers2

1

Take one random upper case letter, one random lower case letter, one random digit, one random symbol and 12 random characters from all groups. Shuffle them (I've copied the shuffle function from How to randomize (shuffle) a JavaScript array?)

function getRandomChar(str) {
  return str.charAt(Math.floor(Math.random() * str.length));
}

function shuffle(array) {
  var currentIndex = array.length,  randomIndex;

  // While there remain elements to shuffle...
  while (currentIndex != 0) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex--;

    // And swap it with the current element.
    [array[currentIndex], array[randomIndex]] = [
      array[randomIndex], array[currentIndex]];
  }

  return array;
}

function generateP(options) {
  const groups = options?.groups ?? [
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
    'abcdefghijklmnopqrstuvwxyz',
    '1234567890',
    '!@#$%^&()_+~`|}{[]:;?><,./-='
  ];
  const length = options?.length ?? 16;
  let pass = groups.map(getRandomChar).join('');
  
  const str = groups.join('');
  
  for (let i = pass.length; i <= length; i++) {
    pass += getRandomChar(str)
  }
  return shuffle(pass);
}

console.log(generateP());

// Tests
console.log('Running tests...');
for (let i = 0; i < 1e5; ++i) {
  const pass = generateP();
  if (!/[A-Z]/.test(pass) || !/[a-z]/.test(pass) || !/[0-9]/.test(pass) || !/[!@#$%^&()_+~`|}{[\]:;?><,./-=]/.test(pass)) {
    console.log('generateP() failed with: ' + pass);
  }
}
console.log('Tests finished');
jabaa
  • 5,844
  • 3
  • 9
  • 30
0

The code below match with your requirements:

var fpwd = "";
var lpwd = 16;

for (;;) {
    var rnd = Math.floor(Math.random() * 128);
    if (rnd >= 33 && rnd <= 126) {
        fpwd += String.fromCharCode(rnd);
    }
    if (fpwd.length == lpwd) {
        if (fpwd.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])((?=.*\W)|(?=.*_))^[^ ]+$/g)) {
        break;
        } else {
           fpwd = "";
        }
    }
}

console.log(fpwd);
Usitha Indeewara
  • 870
  • 3
  • 10
  • 21