showing results for - "custom captcha javascript"
Kenan
15 Aug 2019
1<!DOCTYPE html>
2<html lang="en">
3  <head>
4    <meta charset="UTF-8" />
5    <title>How to create custom Captcha using javascript</title>
6    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7    <meta https-equiv="X-UA-Compatible" content="ie=edge" />
8    <link rel="stylesheet" href="style.css" />
9    <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@500&display=swap" rel="stylesheet">
10  </head>
11<body onload="generateCaptcha();">
12   <div class="full-row">
13      <div class="heading"><h1>How to create custom Captcha using javascript</h1></div>
14      <div class="captcha_outer flex-row">
15         <div class="captcha_output">
16            <input type="text" id="Captcha" readonly>
17         </div>
18         <div class="captcha_gen">
19            <button id="refresh" onclick="generateCaptcha();">Refresh</button>
20         </div>
21      </div>
22      <div class="captcha_valid flex-row">
23         <div class="fillcaptcha"><input type="text" id="txtInput" /></div>
24         <div class="valid_captcha">
25            <button id="CheckCaptcha" onclick="CheckValidCaptcha();">Check</button>
26         </div>
27      </div>
28      <div class="valid-msg-error">
29         <span id="error" style="color:red"></span>
30         <span id="success" style="color:green"></span>
31      </div>
32   </div>
33</body>
34
35<script type="text/javascript">
36   function generateCaptcha() {
37      var alpha = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
38         'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
39         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
40         'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
41      var i;
42      for (i = 0; i < 5; i++) {
43         var a = alpha[Math.floor(Math.random() * alpha.length)];
44         var b = alpha[Math.floor(Math.random() * alpha.length)];
45         var c = alpha[Math.floor(Math.random() * alpha.length)];
46         var d = alpha[Math.floor(Math.random() * alpha.length)];
47         var e = alpha[Math.floor(Math.random() * alpha.length)];
48      }
49      var code = a + '' + b + '' + '' + c + '' + d + '' + e;
50      document.getElementById("Captcha").value = code;
51
52   }
53   function CheckValidCaptcha() {
54      var string1 = removeSpaces(document.getElementById('Captcha').value);
55      var string2 = removeSpaces(document.getElementById('txtInput').value);
56      if (string1 == string2) {
57         document.getElementById('success').innerHTML = "Captcha is validated Successfully";
58         //alert("Form is validated Successfully");
59         return true;
60      }
61      else {
62         document.getElementById('error').innerHTML = "Please enter a valid captcha.";
63         //alert("Please enter a valid captcha.");
64         return false;
65      }
66   }
67   function removeSpaces(string) {
68      return string.split(' ').join('');
69   }
70
71</script>
72
73</html>