JavaScript - Automatically Create Password

Submitted by: 
Language: 
Visitors have accessed this post 190 times.

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="<a href="https://sourcecodester.com">Sourcecodester.com</a>
  12. " rel="nofollow">https://sourcecodester.com">Sourcecodester.com</a>
  13. </a> </div>
  14. </nav>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6 well">
  17. <h3 class="text-primary">JavaScript - Automatically Create Password</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-3"></div>
  20. <div class="col-md-6">
  21. <form class="form-group">
  22. <label>Default User Password</label>
  23. <input type="text" id="password" class="form-control" readonly = "readonly"/>
  24. <br /><br />
  25. <center><button type="button" class="btn btn-primary" onClick="getPassword()"/>Create Password</button></center>
  26. </form>
  27. </div>
  28. </div>
  29. </body>
  30. <script src="js/script.js"></script>
  31. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.