JavaScript - Simple Random Code Generator

Submitted by: 
Language: 
Visitors have accessed this post 354 times.

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 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="<a href="https://sourcecodester.com">Sourcecodester.com</a>
  12. " rel="nofollow">https://sourcecodester.com">Sourcecodester.com</a>
  13. </a> </div>
  14. </nav>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6 well">
  17. <h3 class="text-primary">JavaScript - Simple Random Code Generator</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-4">
  20. <form>
  21. <div class="form-group">
  22. <label>Enter code here</label>
  23. <input type="text" class="form-control" id="ccode"/>
  24. <div id="result"></div>
  25. <br />
  26. <center><button class="btn btn-primary" id="button" type="button" onclick="checkCode(this);" disabled="disabled">Confirm</button></center>
  27. <br />
  28. <center><a href="index.html" class="btn btn-success" id="reset" type="button" style="display:none;">Reset</a> </center>
  29. </div>
  30. </form>
  31. </div>
  32. <div class="col-md-7">
  33. <div id="code" style="background-color:#ccc; height:100px;"></div>
  34. <br />
  35. <center><button type="button" class="btn btn-primary" onclick="getCode(this);"/><span class="glyphicon glyphicon-random"></span> Generate</button></center>
  36. </form>
  37. </div>
  38. </div>
  39. </body>
  40. <script src="js/script.js"></script>
  41. </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='';
  3.  
  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. }
  10.  
  11. function generateCode(len){
  12. code='';
  13. for(i=0; i<len; i++){
  14. code+=keys.charAt(Math.floor(Math.random()*keys.length));
  15. }
  16.  
  17. return code;
  18. }
  19.  
  20. function checkCode(btn){
  21. var val=document.getElementById('ccode').value;
  22.  
  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!


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