How to Create Password Generator

In this tutorial, we are going to create Password Generator using JavaScript. This simple program, you can generate your own password. You can set your own characters and the length manually to generate it into your own password. Creating two TextBox for the length and the character that we are going to generate the password and one button as shown in the image below. Result Here's the source code in the image above.
  1. <div class="container">
  2.   <h4>Password Generator</h4>
  3.   <div class="row">
  4.     <div class="eight columns">
  5.       <p><label for="">Length:</label> <input id="length" type="number" value="12"/> <br />
  6.         <label for="">Characters:</label>
  7.         <input type="text" value='12345677890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPSDFGHJKLZXCVBNM' id="charset" />
  8.         <button id="generate">Generate</button>
  9.       </p>
  10.     </div>
  11.     <div class="four columns" id="passwords">
  12.     </div>
  13.   </div>
  14. </div>
Kindly copy and paste this link and script to your HEAD tag of your page.
  1. <link rel="stylesheet" href="css/style.css">
  2.  
  3. <script src="js/function.js"></script>
  4. <script>
  5.     window.console = window.console || function(t) {};
  6.     window.open = function(){ console.log('window.open is disabled.'); };
  7.     window.print = function(){ console.log('window.print is disabled.'); };
  8.     // Support hover state for mobile.
  9.     if (false) {
  10.       window.ontouchstart = function(){};
  11.     }
  12. </script>
Kindly copy and paste this link and script to your BODY tag of your page.
  1. <script src='js/jquery.js'></script>
  2. <script>
  3.     if (document.location.search.match(/type=embed/gi)) {
  4.       window.parent.postMessage('resize', "*");
  5.     }
  6. </script>
  7. <script src="js/timeout.js"></script>
  8. <script>
  9.     $('#generate').click(function () {
  10.     var charset = $('#charset').val();
  11.     var password = '';
  12.     var password_length = parseInt($('#length').val());
  13.     for (var i = 0; i < password_length; i++) {
  14.         if (window.CP.shouldStopExecution(1)) {
  15.             break;
  16.         }
  17.         var random_position = Math.floor(Math.random() * charset.length);
  18.         password += charset[random_position];
  19.     }
  20.     window.CP.exitedLoop(1);
  21.     if (password.length == password_length) {
  22.         password = password.replace(/</g, '&lt;').replace(/>/g, '&gt;');
  23.         $('#passwords').prepend(password + '<br/>' + '<hr />');
  24.     } else {
  25.         console.log(password.length, password_length, password);
  26.     }
  27. });
  28. </script>
The complete source code.
  1. <!DOCTYPE html>
  2.  
  3.  
  4.   <meta charset="UTF-8">
  5.  
  6.   <title>Password Generator</title>
  7.  
  8. <link rel="stylesheet" href="css/style.css">
  9.  
  10. <script src="js/function.js"></script>
  11.     window.console = window.console || function(t) {};
  12.     window.open = function(){ console.log('window.open is disabled.'); };
  13.     window.print = function(){ console.log('window.print is disabled.'); };
  14.     // Support hover state for mobile.
  15.     if (false) {
  16.       window.ontouchstart = function(){};
  17.     }
  18.  
  19. </head>
  20.  
  21.  
  22. <div class="container">
  23.   <h4>Password Generator</h4>
  24.   <div class="row">
  25.     <div class="eight columns">
  26.       <p><label for="">Length:</label> <input id="length" type="number" value="12"/> <br />
  27.         <label for="">Characters:</label>
  28.         <input type="text" value='12345677890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPSDFGHJKLZXCVBNM' id="charset" />
  29.         <button id="generate">Generate</button>
  30.       </p>
  31.     </div>
  32.     <div class="four columns" id="passwords">
  33.     </div>
  34.   </div>
  35. </div>
  36.  
  37. <script src='js/jquery.js'></script>
  38.     if (document.location.search.match(/type=embed/gi)) {
  39.       window.parent.postMessage('resize', "*");
  40.     }
  41. <script src="js/timeout.js"></script>
  42.     $('#generate').click(function () {
  43.     var charset = $('#charset').val();
  44.     var password = '';
  45.     var password_length = parseInt($('#length').val());
  46.     for (var i = 0; i < password_length; i++) {
  47.        if (window.CP.shouldStopExecution(1)) {
  48.            break;
  49.        }
  50.        var random_position = Math.floor(Math.random() * charset.length);
  51.        password += charset[random_position];
  52.    }
  53.    window.CP.exitedLoop(1);
  54.    if (password.length == password_length) {
  55.        password = password.replace(/</g, '&lt;').replace(/>/g, '&gt;');
  56.         $('#passwords').prepend(password + '<br/>' + '<hr />');
  57.     } else {
  58.         console.log(password.length, password_length, password);
  59.     }
  60. });
  61.  
  62. </body>
  63.  
  64. </html>

Here's the result.

Result Hope that this tutorial will help you a lot. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment