如何使 reCAPTCHA 与 ASP.Net 中的 ValidationGroup 一起使用 (captcha)
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我想我只是用一些工作代码来扩展其他一些人的评论...
以及后面的代码...
有人能想到一种更简单的方法吗? 感谢 Vidalik 对于使用 OnServerValidate 的想法。
Thought I'd just expand on the comments by a few others with some working code...
And code behind...
Can anyone think of a simpler way of doing it? Kudos to Vidalik for the thoughts about using OnServerValidate.
您可以添加 CustomValidator,实现 OnServerValidate 来验证 ReCAPTCHA 数据。 CustomValidator 可以分配给任何 ValidatorGroup。
You can add CustomValidator, implement OnServerValidate that would validate the ReCAPTCHA data. CustomValidator can be assigned to any ValidatorGroup.
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 supportValidationGroup
.In ASP.NET 2.0, validators should inherit from
BaseValidator
and implementIValidator
, which means you should change the RecaptchaControl type to inherit fromBaseValidator
instead ofWebControl
. You will then have to modify the code a bit to implement all methods and properties defined inBaseValidator
. Then you can use this new control on your page instead, which now supportsValidationGroup
.这对我有用...
添加具有正确验证组的自定义验证器。
它的 ServerValidate 方法调用..
然后在主要处理之前检查如下...
if (Page.IsValid && recaptcha.IsValid)
{
respose.write("有效");
}
This worked for me...
Add a custom validator with the correct validation group.
Its its ServerValidate method call..
Then check as follows before your main processing...
if (Page.IsValid && recaptcha.IsValid)
{
respose.write("valid");
}
RemotecUk 的建议对我有用,无需添加自定义验证器。
RemotecUk's suggestion worked for me without adding custom validator.
为了执行客户端所需的验证并且不更改 reCaptcha 源代码,我在表单中添加了 CustomValidator 并创建了一个 JavaScript 函数来验证输入文本字段。
为了找出生成的输入字段的
ID
,我查看了页面的源代码,发现输入字段始终是recaptcha_response_field
。 (如果我错了,请纠正我)知道了这一点,我就能够创建 JavaScript(使用 JQuery 和自定义函数来检查控件的有效性)。注意:如果开发人员更改了 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.
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).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.
请参阅作为 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.