Create Captcha Code in HTML and Javascript


Create Captcha Code in HTML and Javascript

Hi friends, in this tutorial, you will learn how to create captcha code in HTML and Javascript. There are various ways to generate captchas such as Google ReCaptcha and other captchas etc.

Also read, Google Maps Add Marker Using Maps Javascript API and HTML

The logic of captcha code in HTML and Javascript

  • First of all, we will call a function with the javascript onclick event handler to the input captcha field to generate the 4-digit random captcha code. The captcha code will load automatically when the page opens. Each time a captcha will be generated after clicking the refresh button.
  • Now, we will use the javascript onsubmit event handler to the HTML form to validate the captcha. Suppose if a user hits the submit button without entering the captcha code then the function within the submit event handler will return false i.e. it prevents the HTML form to submit and an error message will be shown to the user.
  • If you enter the wrong captcha then you will receive an error message otherwise you will receive a success alert for the correct captcha.

Also, you have to disable the copy text from the captcha field so that the captcha can not be copied. In order to do so, add the below lines of code inside the script tag.

// Disable right click on web page
        $("html").on("contextmenu",function(e){
            return false;
        });
        // Disable cut, copy and paste on web page
        $('html').bind('cut copy paste', function (e) {
             e.preventDefault();
        });

Note:- Do not forget to include the jquery CDN link inside the head tag otherwise you can not disable the copy text feature.

HTML:-

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Captcha Code</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body onload="generateCaptcha(event);">

<div class="container mt-3" style="width:50%;">
  <h2>Captcha Code in HTML and Javascript</h2>
  <form action="/action_page.php" method="post" onsubmit="return validateForm()" name="myForm">
    <div class="mb-3 mt-3">
      <label for="email" class="fw-bold">Email:</label>
      <input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
    </div>
    <div class="mb-3">
      <label for="pwd" class="fw-bold">Password:</label>
      <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pswd">
    </div>
    <div class="mb-3">
      <label for="pwd" class="fw-bold">Captcha:</label>
      <div class="row">
        <div class="col-sm-10">
          <input type="text" name="maincaptcha" readonly class="form-control" id="mainCaptcha"/>
        </div>
        <div class="col-sm-2">
          <button class="btn btn-success btn-sm" onclick="generateCaptcha(event);" id="refresh"><i class="fa fa-refresh" style="font-size:17px"></i></button>
        </div>
      </div>
    </div>
    <div class="mb-3">
      <label for="pwd" class="fw-bold">Enter Captcha:</label>
      <div class="row">
        <div class="col-sm-10">
          <input type="text" name="checkcaptcha" class="form-control" id="txtInput"/>  
        </div>
      </div>
    </div>
    <div class="mb-3">
      <span id="error" style="color:red;"></span>
      <span id="success" style="color:green;"></span>
    </div>

    <button type="submit" class="btn btn-primary btn-sm">Submit</button>
  </form>
</div>
<script type="text/javascript">
  //for captcha
         function generateCaptcha(event)
         {
             event.preventDefault();
             var alpha = new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
             var i;
             for (i=0;i<4;i++){
               var a = alpha[Math.floor(Math.random() * alpha.length)];
               var b = alpha[Math.floor(Math.random() * alpha.length)];
               var c = alpha[Math.floor(Math.random() * alpha.length)];
               var d = alpha[Math.floor(Math.random() * alpha.length)];
              }
            var code = a + '' + b + '' + '' + c + '' + d;
            document.getElementById("mainCaptcha").value = code
            var el = document.getElementById('mainCaptcha');
            el.style.fontFamily = 'Geneva, Verdana, sans-serif';
            el.style.fontSize = 'large';
          }

       //validate the form
       function validateForm() {
          let x = document.forms["myForm"]["checkcaptcha"].value;
          let y = document.forms["myForm"]["maincaptcha"].value;
          
          if (x == "") {
              document.getElementById('error').innerHTML = "Please enter the captcha."; 
               
              return false;
          }
          if(x!==y){
              document.getElementById('error').innerHTML = "Captcha does not match.";
               
              return false; 
          }
          if(x===y){
              document.getElementById('error').innerHTML = ""; 
              alert("Captcha entered successfully");
              return true; 
          }
        }
       // automatically generate captcha after page laod
        $(window).on('load', function () {
            generateCaptcha(event);
        });
          // Disable right click on web page
        $("html").on("contextmenu",function(e){
            return false;
        });
        // Disable cut, copy and paste on web page
        $('html').bind('cut copy paste', function (e) {
             e.preventDefault();
        });
</script>
</body>
</html>

Conclusion:- I hope this tutorial will help you to understand the concept of the captcha code. If there is any doubt then please leave a comment below.


Leave a Comment

KUPU178

KUPU178

RATU311

THOR311 TOTO4D

KUPU178

Live Casino Roullate

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

THOR311

THOR311

THOR311

RATU311

CERI188

DAFTAR KUPU178

LINK DAFTAR CERI188

CERI188

sabung ayam online

APK KUPU178

KUPU178

CERI188

CERI188

RATU311

KUPU178

THOR311 DOMINO QQ

judi bola online

Mix Parlay

CERI188

CERI188

CERI188

KUPU178

RATU311

live casino online

RATU311

Mix Parlay

Sabung Ayam Online

Slot PG Soft

CERI188

RATU311

RATU311

RATU311

RATU311

CERI188

THOR311

CERI188

THOR311

RATU311

THOR311

Game Thor311

THOR311

HOKI311

live casino

sv388

HOKI311

HOKI311

slot mahjong

Thor311 Alternatif

RATU311

HOKI311

HOKI311

HOKI311

HOKI311

CERI188

THOR311 RESMI

THOR311 AKSES

THOR311

THOR311 RTP

RATU311

RATU311

THOR311

parlay

HOKI311

RATU311

THOR311

Ceri188

Thor311 Game Online

Thor311 Aplikasi

LINK CERI188

RATU311 NONTON BOLA

RATU311

CERI188 Link RTP LIVE

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

THOR311 LOGIN

THOR311

THOR311

RATU311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

THOR311

RATU311

THOR311

HOKI311 Link Apk Resmi

KUPU178

RATU311

RATU311

CERI188

CERI188 Situs Spaceman Terbaik 2026

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

CERI188 Live Casino

HOKI311 Lapak Arena Slot

RATU311 judi bola

CERI188 Live Casino

HOKI311 Link Spaceman Online

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

KUPU178

KUPU178

KUPU178

HOKI311 TEMBAK IKAN ONLINE

RATU311

CERI188 PANDUAN MAXWIN SLOT ONLINE

RATU311

RATU311

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

Ceri188 Game Online

CERI188

link login CERI188

link login CERI188

CERI188

link login CERI188

link login CERI188

link daftar CERI188

THOR311

HOKI311

HOKI311

CERI188

CERI188

CERI188

parlay

THOR311

RATU311

RATU311

HOKI311

HOKI311

parlay

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

RATU311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

situs bola

THOR311

slot gacor

slot gacor

yakuza303

slot gacor

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

mix parlay

judi bola

judi bola

mix parlay

parlay

judi bola

sv388

judi bola

judi bola

judi bola

parlay

THOR311