Skip to content

Instantly share code, notes, and snippets.

@madan712
Created February 17, 2013 18:20
Show Gist options
  • Save madan712/4972634 to your computer and use it in GitHub Desktop.
Save madan712/4972634 to your computer and use it in GitHub Desktop.
JSP example - Google Recaptcha validation using jquery (AJAX)
<%
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>
@vanBersie
Copy link

Hey there! Could you please post that validateRecaptcha.jsp as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment