JavaScript - Simple Random Code Generator
Submitted by razormist on Thursday, July 4, 2019 - 21:21.
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.
.
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-4">
- <form>
- <div class="form-group">
- <input type="text" class="form-control" id="ccode"/>
- <br />
- <br />
- </div>
- </form>
- </div>
- <div class="col-md-7">
- <br />
- </form>
- </div>
- </div>
- </body>
- </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- var keys = "abcdefghijklmnopqrstubwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
- var code='';
- function getCode(btn){
- code=generateCode(8);
- document.getElementById('code').innerHTML = "<center><label style='font-size:35px; margin-top:30px;'>"+code+"</label></center>";
- document.getElementById('button').removeAttribute('disabled');
- btn.setAttribute('disabled', 'disabled');
- }
- function generateCode(len){
- code='';
- for(i=0; i<len; i++){
- code+=keys.charAt(Math.floor(Math.random()*keys.length));
- }
- return code;
- }
- function checkCode(btn){
- var val=document.getElementById('ccode').value;
- if(val == ""){
- document.getElementById('result').innerHTML="<center class='text-danger'>Fill up the form first!</center>";
- }else{
- if(code==val){
- document.getElementById('result').innerHTML="<center class='text-success'>The code can be use</center>";
- document.getElementById('reset').style.display='inline';
- }else{
- document.getElementById('result').innerHTML="<center class='text-danger'>Invalid code<center>";
- }
- }
- }
Comments
Add new comment
- Add new comment
- 1620 views