Return to Snippet

Revision: 14496
at June 5, 2009 13:38 by chrisaiv

Initial Code
<script type="text/javascript" charset="utf-8" src="/location/to/jquery.1.3.1.min.js"></script>
<script type="text/javascript" charset="utf-8">
//Validate the Recaptcha' Before continuing with POST ACTION
function validateCaptcha()
	challengeField = $("input#recaptcha_challenge_field").val();
	responseField = $("input#recaptcha_response_field").val();
	//return false;
	var html = $.ajax({
		type: "POST",
		url: "/location/to/ajax.recaptcha.php",
		data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
		async: false

	//console.log( html );
	if(html == "success") {
		//Add the Action to the Form
		$("form").attr("action", "http://action/to/the/form_handler");
		//Indicate a Successful Captcha
		// Uncomment the following line in your application
		return true;
	} else {
		$("#captchaStatus").html("The security code you entered did not match. Please try again.");
		return false;

**    @Title: Recaptcha Validation
**    @Date Modified: 6/01/09
**    Instructions: This code lives within the form.  Generally above the <SUBMIT> button

//A.This div notifies the user whether the Recaptcha was Successful or not
echo "\n\t" . '<div id="captchaStatus" style="color:red;font-size:12px"></div>';

//B. This script provides Recaptcha with a Theme
echo "\n\t" .'<script type="text/javascript" charset="utf-8">';
echo "\n\t" .'var RecaptchaOptions = { theme: "clean" };';
echo "\n\t" . '</script>' . "\n";

//C. Require the Recaptcha Library

recaptcha_get_html( $publickey );

**    @Title: Recaptcha Validation
**    @Date Modified: 6/01/09
**    Instructions: Place this code in a file called "ajax.recaptcha.php"

//A. Load the Recaptcha Libary

$privatekey = "RECAPTCHA_PRIVATE_KEY";

//B. Recaptcha Looks for the POST to confirm 
$resp = recaptcha_check_answer ($privatekey,

//C. If if the User's authentication is valid, echo "success" to the Ajax
if ($resp->is_valid) {
	echo "success";
} else {
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
       "(reCAPTCHA said: " . $resp->error . ")");

Initial URL

Initial Description
I was asked to add Recaptcha' to an already existing form.  I didn't want to interfere with the code so I decided to search for some sort of AJAX solution.  Here's my implementation of  This form needs to have a form with <form>

Initial Title
jQuery: Validating Recaptcha' with AJAX

Initial Tags
ajax, jquery

Initial Language