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>