JavaScript - Generate Serial Code

In this tutorial we will create a Generate Serial Code using JavaScript. This code will generate random serials each time user click the button. This code use onclick function to initiate a javascript built-in function Math.floor() that randomly call a switch cases base on the multiplied integer. 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.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Generate Serial Code</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <button type="button" class="btn btn-primary" onclick="generateSerial();">Generate</button>
  17.                 <br /><br />
  18.                 <div class="col-md-6">
  19.                         <div class="form-group">
  20.                                 <label>Serial 1</label>
  21.                                 <input type="text" class="form-control" id="serial1" readonly="readonly"/>
  22.                         </div>
  23.                 </div>
  24.                 <div class="col-md-6">
  25.                         <div class="form-group">
  26.                                 <label>Serial 2</label>
  27.                                 <input type="text" class="form-control" id="serial2" readonly="readonly"/>
  28.                         </div>
  29.                 <div>  
  30.         </div>
  31. <script src="js/script.js"></script>   
  32. </body>
  33. </html>

Creating the Script

This code contains the script of the application. This code will generate a serials 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 generateSerial(){
  2.         var n=7;
  3.         var number = Math.floor(Math.random()*n)-1;
  4.  
  5.         switch(number){
  6.                 case 0:
  7.                         document.getElementById('serial1').value="tt55-RTJ0-6600-A0A2-08B0-AQS3";
  8.                         document.getElementById('serial2').value="763B-24DF-0943-0084-F778";
  9.                         break;
  10.                 case 1:
  11.                         document.getElementById('serial1').value="qwwe-RTJ0-6600-A0A2-08B0-FD32";
  12.                         document.getElementById('serial2').value="AF21-24D2-7CB0-00A4-4DDC";
  13.                         break;
  14.                 case 2:
  15.                         document.getElementById('serial1').value="1111-22TJ0-6600-A0A0-03B0-FCXS";
  16.                         document.getElementById('serial2').value="4444-24D2-79C2-00A4-0406";
  17.                         break;
  18.                 case 3:
  19.                         document.getElementById('serial1').value="444e-R830-6310-B0A2-0HBH-CDE4";
  20.                         document.getElementById('serial2').value="ffff-24D3-696E-00A4-5FCC";
  21.                         break;
  22.                 case 4:
  23.                         document.getElementById('serial1').value="zzzz-R528-ffre-B0f2-0HW0-FD3D";
  24.                         document.getElementById('serial2').value="5bb7-24DD-775C-0084-A2BE";
  25.                         break;
  26.                 case 5:
  27.                         document.getElementById('serial1').value="vvvv-bbbb-T3A0-TFF2-00B1-QGBD";
  28.                         document.getElementById('serial2').value="4bb8-24F2-5A0C-0082-64AF";
  29.                         break;
  30.                 case 6:
  31.                         document.getElementById('serial1').value="xxxb-ccc-c-A0ccA2-0FGG-GV94";
  32.                         document.getElementById('serial2').value="xxxx-cccc-1111-2422-F2E4D";
  33.                         break; 
  34.         }
  35. }
There you have it we successfully created a Generate Serial Code 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