When we develop any application, we need a random password for every user.As more as secure password is recommended. The conditions for secure password must follow
- Minimum 8 character long password
- Minimum one uppercase letter
- Minimum one lowercase letter
- Minimum one special character
- Minimum one number.
The combination of above condition will generate a secure password.
function generatePassword(length) {
// code to generate passowrd
var charNum = "0123456789";
var charLower = "abcdefghijklmnopqrstuvwxyz"
var charUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var charSpecial = "!@#$%^&*_"
var charAll = charNum + charLower + charUpper + charSpecial;
var password = "";
for (var i = 0; i < length; i++) {
if (i == 0) {
password += charUpper.charAt(Math.floor(Math.random() * charUpper.length));
}
if(i == 1){
password += charLower.charAt(Math.floor(Math.random() * charLower.length));
}
if(i == 2){
password += charNum.charAt(Math.floor(Math.random() * charNum.length));
}
if(i == 3){
password += charSpecial.charAt(Math.floor(Math.random() * charSpecial.length));
}
var char = charAll[Math.floor(Math.random() * charAll.length)];
password += char;
}
return password;
}
Top comments (0)