DEV Community

Cover image for Random Password Generate
Chandra Prakash Pal
Chandra Prakash Pal

Posted on

Random Password Generate

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

  1. Minimum 8 character long password
  2. Minimum one uppercase letter
  3. Minimum one lowercase letter
  4. Minimum one special character
  5. 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; } 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)