JavaScript - Generate Serial Code

Submitted by: 
Visitors have accessed this post 398 times.

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

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="<a href="">Sourcecodester</a>
  10. " rel="nofollow">">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Generate Serial Code</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-primary" onclick="generateSerial();">Generate</button>
  18. <br /><br />
  19. <div class="col-md-6">
  20. <div class="form-group">
  21. <label>Serial 1</label>
  22. <input type="text" class="form-control" id="serial1" readonly="readonly"/>
  23. </div>
  24. </div>
  25. <div class="col-md-6">
  26. <div class="form-group">
  27. <label>Serial 2</label>
  28. <input type="text" class="form-control" id="serial2" readonly="readonly"/>
  29. </div>
  30. <div>
  31. </div>
  32. <script src="js/script.js"></script>
  33. </body>
  34. </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;
  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

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.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.