-2

I have made a simple Password Generator with Javascript

It works fine and perfectly generates a password with 8 characters but the problem with this is that, sometimes the generated password does not contain any symbol which is not good for a strong password.

I need to make sure the final generated password meets these requirements:

1- Must include uppercase letter
2- Must include lowercase letter
3- Must include symbol
4- Must include number

How can I do this with JS?

const myInp = document.querySelector('#myInp');
const password = document.querySelector('#password');
const button = document.querySelector('#generate');
const btnCopy = document.querySelector('#btnCopy');

btnCopy.onclick = function() {
  password.select();

  document.execCommand("Copy");
};

let characters = 'QWERTYUIOPASDFGHJKLZXCVBNM0123456789qwertyuiopasdfghjklzxcvbnm!@#$%^&*/?~(){}[]<>."';
let passwordLength = 8;
let passwordValue = '';

const createPassword = () => {
  passwordValue = '';

  for (let i = 0; i < passwordLength; i++) {
    let number = Math.floor(Math.random() * characters.length);
    passwordValue += characters.substring(number, number + 1);
  }

  password.value = passwordValue;
}

button.addEventListener('click', createPassword);
<div class="form-group">
  <label for="inputPassword" class="col-sm-2 control-label">Make strong password</label>
  <input type="text" class="form-control" id="password" readonly>
  <button id="generate" class="btn btn-primary" type="button">Make</button>
  <button id="btnCopy" class="btn btn-primary" type="button">Copy</button>
</div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
japose7523
  • 29
  • 2
  • 15
  • I made you a snippet. I fixed an obvious error on the BtnCopy – mplungjan Jul 13 '22 at 06:35
  • If you want characters from a certain "character class" to be in the result, then you can not just randomly select from _all_ characters, you need to make a random selection from each class. Fill up with randomly selected characters from "all", after you satisfied all the individual requirements first. – CBroe Jul 13 '22 at 06:36

1 Answers1

1

If you want to generate a password of length 8 that meets your requirements you can use this snippet:

const LENGTH = 8;

const generateRandomPassword = () => {
  const numberChars = "0123456789";
  const lowerChars = "abcdefghijklmnopqrstuvwxyz";
  const upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  const symbolChars = `!@#$%^&*/?~(){}[]<>."'`;

  const allChars = `${numberChars}${lowerChars}${upperChars}${symbolChars}`;
  let password = Array(LENGTH);
  password[0] = numberChars;
  password[1] = lowerChars;
  password[2] = upperChars;
  password[3] = symbolChars;

  password = password.fill(allChars, 4);
  return shuffleArray(
    password.map((x) => x[Math.floor(Math.random() * x.length)])
  ).join("");
};

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

console.log(generateRandomPassword());
sm3sher
  • 2,764
  • 1
  • 11
  • 23