JavaScript - Simple Generate Password For User

In this tutorial we will create a Simple Generate Password For User using JavaScript. This code will create a random password for user each time the button is click. This code use onclick function to initiate a javascript built-in Math function to randomly mix the characters in order to generate a newly string. This program is a user-friendly feel free to use it in your system. We will use JavaScript to allow you to implement complex things on web pages. It enables you to create dynamically updating content and add some interactive visual for the user transactions.

Getting Started:

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">Javascript - Simple Generate Password For User</h3>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-2"></div>
  19.                 <div class="col-md-8">
  20.                         <form class="form-group">
  21.                                 <label>Password Length:</label>
  22.                                 <center><input type="text" id="plength" class="form-control" style="width:200px;"/></center>
  23.                                 <br />
  24.                                 <center><button type="button" class="btn btn-sm btn-success" onClick="getPassword(this)"/><span class="glyphicon glyphicon-random"></span> Create Password</button> <a href="" class="btn btn-sm btn-danger"/><span class="glyphicon glyphicon-reset"></span> Get Password Again</a></center>
  25.                                 <br />
  26.                                 <input type="text" id="password" placeholder="My Password" class="form-control" readonly = "readonly"/>
  27.                         </form>
  28.                 </div>
  29.         </div>
  30. </body>
  31. </body>
  32. <script src="js/script.js"></script>
  33. </html>

Creating the Script

This code contains the script of the application. This code will create a password when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. var chars = "abcdefghijklmnopqrstubwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  2. var password = '';
  3.  
  4.  
  5. function getPassword(btn){
  6.         var plength = document.getElementById('plength').value;
  7.         if(plength == 0){
  8.                 alert("Please enter something first");
  9.         }else{
  10.                 document.getElementById('password').value = createPassword(plength);
  11.                
  12.                 btn.setAttribute('disabled', 'disabled');
  13.         }
  14. }
  15.  
  16. function createPassword(plength){
  17.         password='';
  18.                 for(i=0; i<plength; i++){
  19.                         password+=chars.charAt(Math.floor(Math.random()*chars.length));
  20.                 }
  21.        
  22.         return password;
  23. }
There you have it we successfully created a Simple Generate Password For User 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