CAPTCHA validation with JavaScript

旧街凉风 提交于 2019-12-11 10:56:09

问题


I am trying to validate Captcha using JavaScript. But I could't find any resource about Client API's of Captcha. Do you have any documentation?


回答1:


If you could validate the captcha through JavaScript that would mean the means of finding out the valid code would be readily available in the code that you're passing to the client, which would effectively invalidate the use for a captcha altogether.

The only secure way to achieve this would be to send an AJAX request to the server, which would then validate the code. This way, your validation would be done exactly the way you normally validate the captcha on the server.




回答2:


Validating a captcha with javascript would mean that you would need to have some representation of your captcha text visible in your html source, which is automatically visible to a bot.

I think a possibility if you absolutely have to validate using javascript would be to hash your captcha text server side, load it in a javascript variable, and then validate using the equivalent javascript hashing function.




回答3:


It would be a bad idea to validate the CAPTCHA using JavaScript as a robot could easily beat the CAPTCHA then. If you mean you want to make an Ajax call to submit the entered text that is slightly different.




回答4:


You can use this it works and is very simple

myFunction();
var val=[];        
val.push("VQ7W3");     /** push value in this array  */
val.push("A1234");     /** To maintain image order here   */
val.push("A32BD");   /** if first image 0_capt.jpeg contains value VQ7W3 so push this value into zero(0) index of array   */
val.push("LD673");
val.push("PQ78V");
val.push("MX81W");
var x;
/**This below method generate random number from 0-5
 * name of image starts with that number will set on 
 * captcha location 
 */
function myFunction() {
    x = Math.floor(Math.random() * 6);     // here 6 stand for how many images you are using return 0-6 number.
	$("#imgCaptchaPlace").html("<img id='abc' src='captcha_images/"+x+"_cpt'/>")  ;   
}

/**
 * This below method will call on change of input fields where user enter
 * captcha code value
 */
function chaeckCpatch(id)
{
		if($("#"+id).val()==val[x])
		{
				alert("match");
		}
		else
		{
			alert("No match");
		}
	
}
  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<body>
<!--  below div use to display captcha image -->
<div id="imgCaptchaPlace"></div>

<!-- below textfield where user enter captcha value -->
<input type="text" id="captchaText" onchange="chaeckCpatch(this.id);"/>

<!-- include this JS file -->
<script src="captchByAarif.js" type="text/javascript"></script>
</body>
</html>

You can download captcha images that are used in this example here

https://github.com/aarifa-ds/Captcha-Code




回答5:


Just as a proof of concept... If you send a HASH of captcha string to browser. You could than dynamically HASH the string entered by the user, and compare the two strings.



来源:https://stackoverflow.com/questions/1998341/captcha-validation-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!