Created
February 17, 2013 18:20
-
-
Save madan712/4972634 to your computer and use it in GitHub Desktop.
JSP example - Google Recaptcha validation using jquery (AJAX)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% | |
String remoteip = request.getRemoteAddr(); | |
%> | |
<!doctype html> | |
<html> | |
<head> | |
<title> Recaptcha validation using jquery ajax </title> | |
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> | |
<script type="text/javascript" src="recaptcha_ajax.js"></script> | |
<script type="text/javascript"> | |
function reloadRecaptcha() { | |
var publicKey = "your_public_key"; | |
var div = "recap"; | |
Recaptcha.create(publicKey,div,{theme: "white"}); | |
return false; | |
} | |
function validate() { | |
var challenge = Recaptcha.get_challenge(); | |
var response = Recaptcha.get_response(); | |
var remoteip = "<%=remoteip%>"; | |
$.ajax({ | |
type: "POST", | |
url: "validateRecaptcha.jsp", | |
async: false, | |
data: { | |
remoteip: remoteip, | |
challenge: challenge, | |
response: response | |
}, | |
success: function(resp) { | |
if(resp == "true") { | |
document.getElementById("message").innerHTML = "Perfect!"; | |
} | |
else { | |
document.getElementById("message").innerHTML = "Incorrect Recaptcha! Please try again!"; | |
reloadRecaptcha(); | |
} | |
} | |
}); | |
return false; | |
} | |
</script> | |
</head> | |
<body onload="return reloadRecaptcha();"> | |
<form method="post" onsubmit="return validate();"> | |
<table> | |
<tr> | |
<td colspan="2"> | |
<div id="message" style="color:#ff0000; "></div> | |
<div id="recap"></div> | |
</td> | |
</tr> | |
<tr> | |
<td colspan="2" align="center"> | |
<input type="submit" value="Submit"> | |
</td> | |
</tr> | |
</table> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey there! Could you please post that validateRecaptcha.jsp as well?