Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Captcha Implementation with Javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 726
    Comment on it

    Hello!! 

    Captcha is stands for "Completely Automated Public Turing test to tell Computers and Humans Apart". It is a test for checking that the response is coming from human. People have tried to abuse the site for the sport and the profit. Captcha is used to protect the website from the attack and accessing the website by machines.

    Modern CAPTCHA are design with the three separate abilities:-

    • Invariant Recognition:- It is the availability to recognize the variation in the shape of letters.Their is a lot of variation in the shape of the letter which can identify by the human but not by the computer. Teaching the computer to identify all the variation is about impossible. 
    • Segmentation:- It deals with the space between the two letters. In captcha, their is no white spaces are available and crowded by the letters.
    • Parsing to correctly:-  The computer must identifies each character correctly.

    We can implement the captcha in multiple ways:-

    1) Servlet Implementation

    2) JavaScript Implementation

    JavaScript Implementation:- In javascript, we uses the method to generate the random text and also validate it. Using javascript, we do not need send data to the server for the validation. This helps in time complexity problems. We generate the text on body load. We uses Math.random() function to generating the random character.

    Code:-

    HTML:-

    <tr>  
      <td>&nbsp;Please enter the security code: </td>  
     <td valign="top"> <input type="text" id="txtInput"style="width:99px; height:38px;"/>    
      <input type="text" id="txtCaptcha" size="12"  
           style="background-image:url(/phpdocs/captcha.gif); font-size:30px;text-align:center; border:none;   
           font-weight:bold; font-family: 'Curlz MT'; font-stretch: ultra-expanded" /> </td>  
     </tr>  

    JavaScript:-

     function DrawCaptcha() {  
                var a = 49, b = 65;  
          var c = 100;  
          var d = 70;  
                    document.feedback.reset();  
                    rancolour();  
                    var element = document.getElementById('txtCaptcha');  
                    element.onselectstart = function () { return false; } // ie  
                    element.onmousedown = function () { return false; } // mozilla  
            if (a == 49) {  
              a = 57;  
            }  
                     var main = document.getElementById('txtCaptcha');  
                     main.value ="";       
            var a1 = String.fromCharCode(64+Math.random()*10+1);  
            var b1 =String.fromCharCode(64+(Math.random()*10)+1);  javascript:void(0)
            var c1 = String.fromCharCode(64+(Math.random()*10)+1);  
            var d1 = String.fromCharCode(64+(Math.random()*10)+1);  
            main.value = a1 +" "+ b1 +" "+ c1 + " "+d1+" "+ String.fromCharCode(64+(Math.random()*10)+2)+ " "+String.fromCharCode(64+(Math.random()*10)+1);  
                     //alert(main.value)  
          }  

     

    Output:-

    Now we have the output but here is a problem, we have to remove the space between character for validating it. So, here is the code for removing space:-

    function removeSpaces(string)  
       {  
         return string.split(' ').join(''); 
       } 

    Now, we can validate it using another block of code:-

    ar str1 = removeSpaces(document.getElementById('txtCaptcha').value);  
     var str2 = document.getElementById('txtInput').value;  
     if (str1.toLowerCase() != str2.toLowerCase()) {   
                 alert("Security code entered is not matching.please try again");  

      

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: