Generate Random Characters Using JavaScript

In this tutorial we will create a Generate Random Characters using JavaScript. This code will generate random characters when user click the button. This code use onclick to call a specific method that dynamically generate a random characters with the use of dot notation charAt() that randomly change the character index position 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">Generate Random Characters Using JavaScript Source Code</h3>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-4">
  19.                         <form>
  20.                                 <div class="form-group">
  21.                                         <input type="text" id="password" placeholder="Password here..." class="form-control" readonly = "readonly"/>
  22.                                 </div>
  23.                                 <center><button type="button" class="btn btn-primary" onclick="generatePassword()"/>Generate</button></center>
  24.                         </form>
  25.                 </div>
  26.         </div>
  27. </body>
  28. <script src="js/script.js"></script>
  29. </html>

Creating the Script

This code contains the script of the application. This code will generate a characters 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 keys = "abcdefghijklmnopqrstubwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  2. var password = '';
  3.  
  4.  
  5. function generatePassword(){
  6.         password='';
  7.                 for(i=0; i<12; i++){
  8.                         password+=keys.charAt(Math.floor(Math.random()*keys.length));
  9.                 }
  10.        
  11.         document.getElementById('password').value=password;
  12. }
There you have it we successfully created a Generate Random Characters 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