Javascript - Simple Random Password Generator

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

In this tutorial we will create a Simple Random Password Generator using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It can renders web pages in an interactive and dynamic fashion. It is widely used in designing a stunning website. This code can be used as your converter to your mathematical problem. So Let's do the coding...

Before we started:

And this is the link for the bootstrap that has been used for the layout of the application https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. This code will render application and display the form. To create this just write these block of code inside the text editor and save this as index.htm.

  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 - Simple Random Password Generator</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-1"></div>
  20. <div class="col-md-10">
  21. <form class="form-inline">
  22. <input type="text" id="password" placeholder="Password here..." class="form-control" readonly = "readonly"/>
  23. <label>Password Length:</label> <input type="text" id="plength" class="form-control" size="2"/>
  24. <br /><br />
  25. <center><button type="button" class="btn btn-primary" onClick="GetPassword()"/><span class="glyphicon glyphicon-random"></span> Generate</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 generate a password characters where the length is base on the input type value. To do this just copy write the code as shown below inside the text editor and save it as script.js.

  1. var keys = "abcdefghijklmnopqrstubwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  2. var password = '';
  3.  
  4.  
  5. function GetPassword(){
  6. var plength = document.getElementById('plength').value;
  7. document.getElementById('password').value = GeneratePassword(plength);
  8. }
  9.  
  10. function GeneratePassword(plength){
  11. password='';
  12. for(i=0; i<plength; i++){
  13. password+=keys.charAt(Math.floor(Math.random()*keys.length));
  14. }
  15.  
  16. return password;
  17. }

There you have it we successfully created a Simple Random Password Generator
using javascript. I hope that this very 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.