Randomly Generate User Password Using JavaScript

In this article we will create Randomly Generate User Password using JavaScript. The program will randomly generate a user password. This is a free code, you are free modify this. To learn more about this, just follow the steps below.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.  
  8. <body>
  9.         <nav class="navbar navbar-default">
  10.                 <div class="container-fluid">
  11.                         <a class="navbar navbar-brand" href="https://sourcecodester.com">Sourcecodester.com</a>
  12.                 </div>
  13.         </nav>
  14.         <div class="col-md-3"></div>
  15.         <div class="col-md-6 well">
  16.                 <h3 class="text-primary">Randomly Generate User Password Using JavaScript</h3>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-6">
  19.                         <form>
  20.                                 <div class="form-group">
  21.                                         <label>Username</label>
  22.                                         <input type="text" class="form-control" style="width:68%;"/>
  23.                                 </diiv>
  24.                                 <div class="form-inline">
  25.                                         <label>Password</label>
  26.                                         <br />
  27.                                         <input type="text" id="password" class="form-control" readonly = "readonly"/>
  28.                                         <button type="button" class="btn btn-success btn-sm" onClick="getPassword()"/>Generate</button>
  29.                                 </diiv>
  30.                                 <br /><br />
  31.                                 <center><button class="btn btn-primary" onclick="alert('Registered')">Register</button></center>
  32.                         </form>
  33.                 </div>
  34.         </div>
  35. </body>
  36. <script src="js/script.js"></script>
  37. </html>

Creating the Script

This code contains the script of the application. The code will automatically generate a password when user click the button. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. function generate(plength){
  2.         var chars = "abcdefghijklmnopqrstubwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  3.         var password = '';     
  4.                 for(i=0; i<plength; i++){
  5.                         password+=chars.charAt(Math.floor(Math.random()*chars.length));
  6.                 }
  7.        
  8.         return password;
  9. }
  10.  
  11. function getPassword(){
  12.         document.getElementById('password').value = generate(12);
  13. }
There you have it we successfully created a Randomly Generate User Password using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment