Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make reCAPTCHA work with a ValidationGroup in ASP.Net (captcha)

I am using the ASP.Net plugin and control provided by reCAPTCHA. I can successfully get the control to work if the submit button on the web form is not in a validationgroup. There is no validationgroup attribute for the reCAPTCHA control.

Has anybody had any success with this or any solutions to get the reCAPTCHA control to work when there is a validationgroup on the web form?

like image 438
410 Avatar asked Oct 23 '08 20:10

410


3 Answers

Thought I'd just expand on the comments by a few others with some working code...

<recaptcha:RecaptchaControl ID="RecaptchaControl" runat="server" />

<asp:CustomValidator ID="RecaptchaValidator" runat="server" OnServerValidate="RecaptchaValidator_ServerValidate" ErrorMessage="Recaptcha input invalid." ValidationGroup="SomeValidationGroup" />

And code behind...

protected void RecaptchaValidator_ServerValidate(object sender, ServerValidateEventArgs e)
{
    this.RecaptchaControl.Validate();
    e.IsValid = this.RecaptchaControl.IsValid;
}

Can anyone think of a simpler way of doing it? Kudos to Vidalik for the thoughts about using OnServerValidate.

like image 160
James Law Avatar answered Nov 13 '22 11:11

James Law


You can add CustomValidator, implement OnServerValidate that would validate the ReCAPTCHA data. CustomValidator can be assigned to any ValidatorGroup.

like image 29
Vitalik Avatar answered Nov 13 '22 13:11

Vitalik


The reCAPTCHA ASP.NET plug-in is written to be backward-compatible with ASP.NET 1.1, which means the ValidationGroup concept (which is new in ASP.NET 2.0) is not supported. But the plug-in comes with downloadable source code, so you can modify it yourself to support ValidationGroup.

In ASP.NET 2.0, validators should inherit from BaseValidator and implement IValidator, which means you should change the RecaptchaControl type to inherit from BaseValidator instead of WebControl. You will then have to modify the code a bit to implement all methods and properties defined in BaseValidator. Then you can use this new control on your page instead, which now supports ValidationGroup.

like image 4
bzlm Avatar answered Nov 13 '22 12:11

bzlm