如何使 reCAPTCHA 与 ASP.Net 中的 ValidationGroup 一起使用 (captcha)

发布于 2024-07-08 08:17:43 字数 219 浏览 7 评论 0 原文

我正在使用 reCAPTCHA 提供的 ASP.Net 插件和控件。 如果 Web 表单上的提交按钮不在验证组中,我可以成功地使控件正常工作。 reCAPTCHA 控件没有验证组属性。

当网络表单上存在验证组时,是否有人成功地使用了此方法或任何解决方案来使 reCAPTCHA 控件正常工作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

魄砕の薆 2024-07-15 08:17:43

我想我只是用一些工作代码来扩展其他一些人的评论...

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

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

以及后面的代码...

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

有人能想到一种更简单的方法吗? 感谢 Vidalik 对于使用 OnServerValidate 的想法。

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.

但可醉心 2024-07-15 08:17:43

您可以添加 CustomValidator,实现 OnServerValidate 来验证 ReCAPTCHA 数据。 CustomValidator 可以分配给任何 ValidatorGroup。

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

乖乖 2024-07-15 08:17:43

reCAPTCHA ASP.NET 插件被编写为向后兼容 ASP.NET 1.1,这意味着不支持 ValidationGroup 概念(这是 ASP.NET 2.0 中的新增功能)。 但该插件附带可下载源代码,因此您可以自己修改它以支持ValidationGroup

在 ASP.NET 2.0 中,验证器应继承自BaseValidator 并实现 IValidator,这意味着您应该将 RecaptchaControl 类型更改为从 BaseValidator 继承,而不是 WebControl. 然后,您必须稍微修改代码以实现 BaseValidator 中定义的所有方法和属性。 然后您可以在页面上使用这个新控件,它现在支持 ValidationGroup

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.

不离久伴 2024-07-15 08:17:43

这对我有用...

  1. 添加具有正确验证组的自定义验证器。

  2. 它的 ServerValidate 方法调用..

     recaptcha.Validate(); 
      
  3. 然后在主要处理之前检查如下...

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

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");
    }

风月客 2024-07-15 08:17:43

RemotecUk 的建议对我有用,无需添加自定义验证器。

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
    }
}

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
    }
}
溺深海 2024-07-15 08:17:43

为了执行客户端所需的验证并且不更改 reCaptcha 源代码,我在表单中添加了 CustomValidator 并创建了一个 JavaScript 函数来验证输入文本字段。

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

为了找出生成的输入字段的 ID,我查看了页面的源代码,发现输入字段始终是recaptcha_response_field。 (如果我错了,请纠正我)知道了这一点,我就能够创建 JavaScript(使用 JQuery 和自定义函数来检查控件的有效性)。

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

注意:如果开发人员更改了 reCaptcha 控件的输出,您可能不会意识到导致验证器开始工作的更改。

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.

没有你我更好 2024-07-15 08:17:43

请参阅作为 Altairis Web UI Toolkit 一部分的 ReCaptchaImage 和 ReCaptchaValidator 控件: http://altairiswebui.codeplex.com/

开源的 Web 组件集,包含相当不错且符合 ASP.NET 标准的(如果我可以说它是作者的话:-) Web 表单的 ReCaptcha 实现。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文