How to Generate a Random Password for Multiple User in JavaScript

How to Generate a Random Password for Multiple User in JavaScript


In this tutorial we will create a How to Generate a Random Password for Multiple User in JavaScript. This tutorial purpose is to teach you how to generate a random password for users. This will cover all the basic function that will update your array data. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that require password for login. I will give my best to provide you the easiest way of creating this program Random Password for all user. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will display the buttons and tables with array object. To create this simply copy and write it into your text editor, then save it 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. <body onload="displayUser();">
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar navbar-brand" href="">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">How to Generate a Random Password for Multiple User</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-default" onclick="generatePasswords();">Create Password</button>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th>Name</th>
  23. <th>Username</th>
  24. <th>Password</th>
  25. </tr>
  26. </thead>
  27. <tbody id="result"></tbody>
  28. </table>
  29. </div>
  30. <script src="script.js"></script>
  31. </body>
  32. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically update the data of an array object when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. var names=["John Smith", "Claire Temple", "Steve Roger", "Jessie Mendoza", "Richard Max"];
  2. var username=["john", "claire12", "steve114", "jessie0001", "rich23"];
  3. var password=[];
  5. function displayUser(){
  6. var html="";
  8. for(var i=0; i<5; i++){
  9. html+="<tr><td>"+names[i]+"</td><td>"+username[i]+"</td><td></td></tr>";
  10. }
  11. document.getElementById('result').innerHTML=html;
  12. }
  14. function generatePasswords(){
  15. var html="";
  17. for(var i=0; i<5; i++){
  18. password.push(randomPassword());
  19. }
  21. for(var i=0; i<5; i++){
  22. html+="<tr><td>"+names[i]+"</td><td>"+username[i]+"</td><td>"+password[i]+"</td></tr>";
  23. }
  24. document.getElementById('result').innerHTML=html;
  26. password=[];
  27. }
  29. function randomPassword(){
  30. var characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  31. var newPassword = "";
  33. for(var i = 0, j = characters.length; i < 12; i++){
  34. newPassword += characters.charAt(Math.floor(Math.random() * j));
  35. }
  37. return newPassword;
  38. }

In the code above we will first create a method that we will call randomPassword(), this function will generate random characters that will act as the password. We use loop to loop thought the character and use the Math() function to get a randomize result. In order to display the password we will create a method called generatePasswords(), this function will append the random password to the html table when the button is triggered.


The How to Generate a Random Password for Multiple User in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Generate a Random Password for Multiple User in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment