Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

This worked for me...

  1. Add a custom validator with the correct validation group.

  2. Its its ServerValidate method call..

    recaptcha.Validate();
    
  3. Then check as follows before your main processing...

    if (Page.IsValid && recaptcha.IsValid) { respose.write("valid"); }

HTH.

RemotecUk's suggestion worked for me without adding custom validator.

protected void button_onclick(object sender, EventArgs e){
    recaptcha.Validate();
    if(!Page.IsValid && recaptcha.IsValid){
        lblError.Text = "Please check your captcha entry";
    } else {
        //do your thing
    }
}

To do client-side required validation and without altering the reCaptcha source code, I added a CustomValidator to my form and created a JavaScript function to validate the input text field.

<asp:CustomValidator ID="reqRecaptcha" runat="server" ClientValidationFunction="validateRecaptcha" Text="Required"></asp:CustomValidator>

To find out the ID of the generated input field I looked at the source code of the page and noticed that the input field was alwaysrecaptcha_response_field. (Please correct me if I am wrong) Knowing this, I was able to create the JavaScript (using JQuery and a custom function to check validity of a control).

    function validateRecaptcha(sender, args) {
        args.IsValid = isFieldValid("input[id$='recaptcha_response_field']");
    }

NOTE: If the developers change the output of the reCaptcha control, you may not be aware of the change resulting in the validator seizing to work.

See ReCaptchaImage and ReCaptchaValidator controls being part of Altairis Web UI Toolkit: http://altairiswebui.codeplex.com/

It's open source set of web components, containing quite decent and ASP.NET standards-compliant (if I may say it being the author :-) implementation of ReCaptcha for Web Forms.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top