Sunday, 12 April 2015

What is Captcha Code? Implimentation of Captcha in Javascript.

Captcha Code: A CAPTCHA (an acronym for "Completely Automated Public Turing test to tell Computers and Humans Apart") is a type of challenge-response test used in computing to determine whether or not the user is human.

Captcha.html: 

<html>
<head>
  <script type="text/javascript" >
    function DrawCaptcha()
    {
        var a = Math.ceil(Math.random() * 10)+ '';
        var b = Math.ceil(Math.random() * 10)+ '';      
        var c = Math.ceil(Math.random() * 10)+ ''; 
        var d = Math.ceil(Math.random() * 10)+ ''; 
        var e = Math.ceil(Math.random() * 10)+ ''; 
        var f = Math.ceil(Math.random() * 10)+ ''; 
        var g = Math.ceil(Math.random() * 10)+ ''; 
        var code = a + ' ' + b + ' ' + ' ' + c + ' ' + d + ' ' + e + ' '+ f + ' ' + g;
        document.getElementById("Captcha").value = code
    }
function ValidCaptcha()
      {
        var str1 = removeSpaces(document.getElementById('Captcha').value);
        var str2 = removeSpaces(document.getElementById('InputCaptcha').value);
               
        if (str1 == str2)
        {
      alert("Correct");
      } 
             
         else
         {    
         alert("Wrong Entry");
         }
     
                     
    }

    // Remove the spaces from the entered and generated code
    function removeSpaces(string)
    {
        return string.split(' ').join('');
    }
    </script>
</head>

<body onload="DrawCaptcha()">
<form>
Captcha Code:<input type="text" id="Captcha"/>
<input type="button" value="Refresh" onclick="DrawCaptcha()"/>
<br/>Enter code: <input type="text" id="InputCaptcha" />
<br/>
<input type="button" value="Check" onclick="ValidCaptcha()"/>
</form>
</body>

</html>



Output: