JavaScript - Automatically Create Password

In this tutorial we will create a Automatically Create Password using JavaScript. This code will create a password automatically the button is click. This code use onclick to call a specific method that automatically create a usable password for user with the use of Math.floor() that randomly generate a new character one by one base on the given length. 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 - Automatically Create Password</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6">
  20. <form class="form-group">
  21. <label>Default User Password</label>
  22. <input type="text" id="password" class="form-control" readonly = "readonly"/>
  23. <br /><br />
  24. <center><button type="button" class="btn btn-primary" onClick="getPassword()"/>Create Password</button></center>
  25. </form>
  26. </div>
  27. </div>
  28. </body>
  29. <script src="js/script.js"></script>
  30. </html>

Creating the Script

This code contains the script of the application. This code will automatically 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. function getPassword(){
  2. document.getElementById('password').value = autoCreate(12);
  3. }
  4.  
  5. function autoCreate(plength){
  6. var chars = "abcdefghijklmnopqrstubwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  7. var password = '';
  8. for(i=0; i<plength; i++){
  9. password+=chars.charAt(Math.floor(Math.random()*chars.length));
  10. }
  11.  
  12. return password;
  13. }
There you have it we successfully created a Automatically Create 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