<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Brandon&#039;s Blog &#187; captcha</title>
	<atom:link href="http://www.brandonturner.net/blog/tag/captcha/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brandonturner.net/blog</link>
	<description>Random thoughts on web applications, software development and Linux</description>
	<lastBuildDate>Thu, 08 Apr 2010 01:47:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Reloading reCAPTCHA with error via Javascript</title>
		<link>http://www.brandonturner.net/blog/2009/02/reload_recaptcha_with_error/</link>
		<comments>http://www.brandonturner.net/blog/2009/02/reload_recaptcha_with_error/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 21:04:36 +0000</pubDate>
		<dc:creator>Brandon</dc:creator>
				<category><![CDATA[Web Applications]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.brandonturner.net/blog/?p=104</guid>
		<description><![CDATA[Today I needed to add a captcha to a web form used to send emails. Ordinarily this is pretty simple using the reCAPTCHA service. There was only one problem: the form was validated and submitted via AJAX rather than a regular HTTP post. The reCAPTCHA client API provides a reload javascript method that will grab [...]]]></description>
			<content:encoded><![CDATA[<p>Today I needed to add a captcha to a web form used to send emails.  Ordinarily this is pretty simple using the <a href="http://www.recaptcha.net">reCAPTCHA</a> service.  There was only one problem: the form was validated and submitted via AJAX rather than a regular HTTP post.</p>
<p>The reCAPTCHA <a href="http://recaptcha.net/apidocs/captcha/client.html">client API</a> provides a <code>reload</code> javascript method that will grab a new set of words for the user to decode.  It was easy enough to display a small error message above the reCAPTCHA div and call this method when the user responds incorrectly.</p>
<p>In a typical setup, as described in the reCAPTCHA <a href="http://recaptcha.net/apidocs/captcha/client.html">client API</a>, the standard theme can display an error message when the user responds incorrectly.  This is done by appending an <code>error</code> URL parameter to the challenge.  The error message I was adding via javascript was showing up above the reCAPTCHA module rather than just above the text box.  I wanted to display the error message included in the standard reCAPTCHA theme.</p>
<p>Though I&#8217;m sure there are several ways to accomplish my goal, I settled on a very easy, but very unsupported solution (read: HACK):</p>
<p>The message the standard template displays just above the input textbox is controlled by a class set on the <code>recaptcha_widget_div</code> element.  A class of <code style="color: #008000">recaptcha_nothad_incorrect_sol</code> causes &#8220;Type the two words:&#8221; or &#8220;Type what you hear:&#8221; to be displayed while a <code style="color: #008000">recaptcha_had_incorrect_sol</code> class causes &#8220;Incorrect. Try again.&#8221; to be displayed.  So to display the default error message all you need to do is change the class of the <code>recaptcha_widget_div</code> element.</p>
<p>Trying to mix this with the reCAPTCHA <code>reload</code> method doesn&#8217;t work as you might expect.  The <code>reload</code> method causes an AJAX request to be sent to the reCAPTCHA server.  When the response arrives, the reCAPTCHA module is updated.  If you modify the class before the response arrives, your updated class will be overwritten.  To fix this, you can override the default response callback <code>Recaptcha.finish_reload</code>.</p>
<p>So my final solution (hack) looked something like this (please note, the page in question was already loading the prototype library so I used it):</p>
<pre class="brush: jscript;">
if(typeof Recaptcha.old_finish_reload === 'undefined') {
   Recaptcha.old_finish_reload = Recaptcha.finish_reload;
   Recaptcha.finish_reload = function(a, b, c, d) {
      Recaptcha.old_finish_reload(a, b, c, d);
      $('recaptcha_widget_div').removeClassName('recaptcha_nothad_incorrect_sol');
      $('recaptcha_widget_div').addClassName('recaptcha_had_incorrect_sol');
   };
}
Recaptcha.reload();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.brandonturner.net/blog/2009/02/reload_recaptcha_with_error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
