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

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay