Skip to main content

JavaScript - Simple Random Code Generator

In this tutorial we will create a Simple Random Code Generator using JavaScript. This code will generate a random code when the user click the generate button. The code itself use a onclick() function to initiate a method that generate a random character by using charAt() in order to randomize the string insert position. You can apply this script to your code, it is a user-friendly program feel free to modify it.

We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting Started:

First you have to download bootstrap framework, 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>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar navbar-brand" href=""></a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">JavaScript - Simple Random Code Generator</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4">
  19. <form>
  20. <div class="form-group">
  21. <label>Enter code here</label>
  22. <input type="text" class="form-control" id="ccode"/>
  23. <div id="result"></div>
  24. <br />
  25. <center><button class="btn btn-primary" id="button" type="button" onclick="checkCode(this);" disabled="disabled">Confirm</button></center>
  26. <br />
  27. <center><a href="index.html" class="btn btn-success" id="reset" type="button" style="display:none;">Reset</a> </center>
  28. </div>
  29. </form>
  30. </div>
  31. <div class="col-md-7">
  32. <div id="code" style="background-color:#ccc; height:100px;"></div>
  33. <br />
  34. <center><button type="button" class="btn btn-primary" onclick="getCode(this);"/><span class="glyphicon glyphicon-random"></span> Generate</button></center>
  35. </form>
  36. </div>
  37. </div>
  38. </body>
  39. <script src="js/script.js"></script>
  40. </html>

Creating the Script

This code contains the script of the application. This code will generate a random code 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 code='';
  4. function getCode(btn){
  5. code=generateCode(8);
  6. document.getElementById('code').innerHTML = "<center><label style='font-size:35px; margin-top:30px;'>"+code+"</label></center>";
  7. document.getElementById('button').removeAttribute('disabled');
  8. btn.setAttribute('disabled', 'disabled');
  9. }
  11. function generateCode(len){
  12. code='';
  13. for(i=0; i<len; i++){
  14. code+=keys.charAt(Math.floor(Math.random()*keys.length));
  15. }
  17. return code;
  18. }
  20. function checkCode(btn){
  21. var val=document.getElementById('ccode').value;
  23. if(val == ""){
  24. document.getElementById('result').innerHTML="<center class='text-danger'>Fill up the form first!</center>";
  25. }else{
  26. if(code==val){
  27. document.getElementById('result').innerHTML="<center class='text-success'>The code can be use</center>";
  28. document.getElementById('reset').style.display='inline';
  29. }else{
  30. document.getElementById('result').innerHTML="<center class='text-danger'>Invalid code<center>";
  31. }
  32. }
  33. }

There you have it we successfully created a Simple Random Code Generator 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.