over 8 years ago
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:-
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> 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>
<tr>
<td> 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)
}
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:-
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");
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");
Can you help out the community by solving one of the following Javascript problems?
Do activity (Answer, Blog) > Earn Rep Points > Improve Rank > Get more opportunities to work and get paid!
For more topics, questions and answers, please visit the Tech Q&A page.
0 Comment(s)