Captcha Generator using HTML, CSS and JavaScript – Coding Torque

Share your love

Welcome to Coding Torque, fellow coders! Are you looking to increase the security of your website or web application? In this tutorial, we will be creating a Captcha Generator using HTML, CSS, and JavaScript. Captchas, or Completely Automated Public Turing tests to tell Computers and Humans Apart, are a popular security measure used to protect websites from spam and automated bot attacks. By the end of this tutorial, you will have a working Captcha generator that you can easily implement on your own website. So, let’s get started and add an extra layer of protection to your online projects!

Before we start, here are some JavaScript Games you might like to create:

1.Ā Snake Game using JavaScript

2.Ā 2D Bouncing Ball Game using JavaScript

3.Ā Rock Paper Scissor Game using JavaScript

4.Ā Tic Tac Toe Game using JavaScript

5. Whack a Mole Game using JavaScript


I would recommend you don’t just copy and paste the code, just look at the code and type by understanding it.



Starter Template

<!doctype html>
<html lang="en">

    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- FONT AWESOME CDN  -->
    <link rel="stylesheet" href=""
        crossorigin="anonymous" />

    <!-- CSS -->
    <link rel="stylesheet" href="style.css">

    <title>Catpcha Generator using JavaScript - @code.scientist x @codingtorque</title>

<body onload="createCaptcha()">
    <!-- Further code here -->

    <script src="script.js"></script>


Paste the below code in your <body> tag

<div class="container">
    <div style="width:100%; display: flex; justify-content: space-between;">
        <div id="captcha">
        <button class="regenerateCaptchaBtn" style="width: 5rem;" onclick="createCaptcha()">
            <i class="fas fa-redo"></i>
    <input type="text" placeholder="Enter Captcha" id="cpatchaTextBox" />
    <button onclick="validateCaptcha()">Submit</button>

Output Till Now

CSS CodeĀ 

Create a fileĀ style.css and paste the code below.

body {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: 10rem;
  background-color: #2f66f5;

.container {
  width: 20rem;
  background: white;
  border-radius: 10px;
  padding: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.1),
    0 8px 10px -6px rgb(0 0 0 / 0.1);

#captcha {
  border-radius: 5px;
  border: 1px solid gainsboro;
  display: flex;
  align-items: center;
  justify-content: center;

input[type="text"] {
  padding: 12px 20px;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 5px;
  box-sizing: border-box;
  width: 100%;
  margin: 12px 0;

button {
  background-color: #2f66f5;
  border: none;
  color: white;
  padding: 12px 30px;
  text-decoration: none;
  margin: 4px 2px;
  cursor: pointer;
  width: 100%;
  border-radius: 5px;

canvas {
  pointer-events: none;


Output Till Now

JavaScript CodeĀ 

Create a fileĀ script.jsĀ and paste the code below.
var code;

function createCaptcha() {
    // clear the contents of captcha div first 
    document.getElementById('captcha').innerHTML = "";
    var charsArray =
    var lengthOtp = 6;
    var captcha = [];
    for (var i = 0; i < lengthOtp; i++) {
        //below code will not allow Repetition of Characters
        var index = Math.floor(Math.random() * charsArray.length + 1); //get the next character from the array
        if (captcha.indexOf(charsArray[index]) == -1)
        else i--;
    var canv = document.createElement("canvas"); = "captcha";
    canv.width = 100;
    canv.height = 50;
    var ctx = canv.getContext("2d");
    ctx.font = "25px Georgia";
    ctx.strokeText(captcha.join(""), 0, 30);
    //storing captcha so that can validate you can save it somewhere else according to your specific requirements
    code = captcha.join("");
    document.getElementById("captcha").appendChild(canv); // adds the canvas to the body element

function validateCaptcha() {
    if (document.getElementById("cpatchaTextBox").value == code) {
        alert("Valid Captcha")
    } else {
        alert("Invalid Captcha. try Again");
Written by: Piyush Patil
Code Credits:Ā @manishjanky
If you have any doubts or any project ideas feel free to Contact Us
Hope you find this post helpfulšŸ’–
Share your love