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.
<!DOCTYPE html>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
<nav class="navbar navbar-default">
<div class="container-fluid">
<a class="navbar navbar-brand" href="https://sourcecodester.com">Sourcecodester.com
</a>
</nav>
<div class="col-md-6 well">
<h3 class="text-primary">JavaScript - Simple Random Code Generator
</h3>
<hr style="border-top:1px dotted #ccc;"/>
<input type="text" class="form-control" id="ccode"/>
<center><button class="btn btn-primary" id="button" type="button" onclick="checkCode(this);" disabled="disabled">Confirm
</button></center>
<center><a href="index.html" class="btn btn-success" id="reset" type="button" style="display:none;">Reset
</a> </center>
<div id="code" style="background-color:#ccc; height:100px;"></div>
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>";
}
}
}
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!