Reputation: 3200
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?
Upvotes: 10
Views: 12838
Reputation: 10772
This worked for me...
Add a custom validator with the correct validation group.
Its its ServerValidate method call..
recaptcha.Validate();
Then check as follows before your main processing...
if (Page.IsValid && recaptcha.IsValid) { respose.write("valid"); }
Upvotes: 2
Reputation: 3811
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.
Upvotes: 1
Reputation: 6656
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
}
}
Upvotes: 1
Reputation: 6733
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.
Upvotes: 7
Reputation: 9
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.
Upvotes: 0
Reputation: 2754
You can add CustomValidator, implement OnServerValidate that would validate the ReCAPTCHA data. CustomValidator can be assigned to any ValidatorGroup.
Upvotes: 5
Reputation: 9727
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
.
Upvotes: 4