How to develop a Random Password Generator using JavaScript?

How to develop a Random Password Generator using JavaScript?

Creating a Random Password Generator: HTML, CSS & JS

ยท

2 min read

In this article i will help you develop a simple web project titled Random Password Generator using HTML, CSS and JavaScript.

Final Output

Youtube Video

For detailed explanation of code watch below video.

Code

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Random Password Generator</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>Random Password Generator</h1>
   <div class="container">
    <div class="form-group">
        <label for="length">Password Length</label>
        <input type="number" min="6" max="20" value="8" id="length">
    </div>
    <button onclick="generatePassword()">Generate Password</button>
    <div class="password">
        <p id="passwordResult"></p>
    </div>
</div>
    <script src="script.js"></script>
</body>
</html>

CSS

body{
    font-family: Arial,sans-serif;
    text-align: center;
    margin-top: 50px;
}

h1{
    color:#F95959;
}

.container{
    max-width: 400px;
    margin:0 auto;
    padding: 20px;
    background-color: #f4f4f4;
    border: 1px solid #ddd;
    border-radius: 5px;
}

.form-group{
    margin-bottom: 10px;
    color:#F95959;
}

label{
    display: block;
    font-weight: bold;
    margin-bottom: 5px;
}

input[type="number"]{
    width:100%;
    padding:5px;
    font-size: 16px;
    border: 1px solid #ccc;
    border-radius:4px;
}

button{
    padding:10px 20px;
    font-size: 16px;
    background-color: #4CAF50;
    color:#fff;
    border:none;
    border-radius: 4px;
    cursor:pointer;
}


.password{
    margin-top:20px;
    color:#F95959;
}

JavaScript

function generatePassword(){
   var length =  document.getElementById('length').value;
   var charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+~`|}{[]\:;?><,./-='; 
   var password = '';
   for(var i=0,n=charset.length;i<length;++i){
    console.log(Math.floor(Math.random()*n));
    console.log(charset.charAt(Math.floor(Math.random()*n)))
    password += charset.charAt(Math.floor(Math.random()*n));
   }
   document.getElementById('passwordResult').innerText = password;
}

I hope you find this article helpful.

Please don't forget to like, comment and share and you can also check out my other articles. Also, don't forget to subscribe to my newsletter so you can be notified about new articles.

I am looking to get into technical writing so if you want me to write an article then please connect with me on my Twitter handle here.

Let me also know which topic I should write about next.

Did you find this article valuable?

Support shubham lashkan by becoming a sponsor. Any amount is appreciated!

ย