如何在单个页面上显示多个验证码?
我在一页上有 2 个表格。 其中一个表单始终显示 Recaptcha。 另一个应该仅在特定事件(例如登录尝试次数达到上限)后显示验证码。 因此,有时我需要 2 个验证码出现在同一页面上。 这可能吗? 我知道我可能可以为两者使用一个,但就我的布局而言,我更喜欢使用 2 个。谢谢。
更新:好吧,我想这可能是不可能的。 有人可以推荐另一个与 reCaptcha 一起使用的捕获库吗? 我真的希望能够在同一页面上有 2 个验证码。
更新 2:如果我将每个表单放入 iframe 中会怎样? 这是一个可以接受的解决方案吗?
I have 2 forms on a single page. One of the forms has a Recaptcha displaying all the time. The other should display a Recaptcha only after a certain event such as maxing out login attempts. So there are times when I would need 2 Recaptchas to appear on the same page. Is this possible? I know I could probably use a single one for both, but the way I have the layout, I would much prefer to have 2. Thanks.
Update: well I guess it may not be possible. Can anybody recommend another capture library to use side by side with reCaptcha? I really want to be able to have 2 captchas on the same page.
Update 2: What if I put each form in an iframe? Would this be an acceptable solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
我知道这个问题已经很老了,但以防万一将来有人会寻找它。 一页上可以有两个验证码。 粉色文档位于此处:https://developers.google.com/recaptcha/docs/display
下面的示例只是一个副本表单文档,您不必指定不同的布局。
I know this question is old but in case if anyone will look for it in the future. It is possible to have two captcha's on one page. Pink to documentation is here: https://developers.google.com/recaptcha/docs/display
Example below is just a copy form doc and you dont have to specify different layouts.
grecaptcha.getResponse()
方法接受可选的“widget_id”参数,如果未指定,则默认为创建的第一个小部件。 每个创建的小部件的grecaptcha.render()
方法都会返回一个 widget_id,它与 reCAPTCHA 容器的属性id
无关!!reCAPTCHA 都有自己的响应数据。
您必须为 reCAPTCHA div 提供一个 ID 并将其传递给
getResponse
方法:例如
访问响应:
或
The
grecaptcha.getResponse()
method accepts an optional "widget_id" parameter, and defaults to the first widget created if unspecified. A widget_id is returned from thegrecaptcha.render()
method for each widget created, it is not related to the attributeid
of the reCAPTCHA container!!Each reCAPTCHA has its own response data.
You have to give the reCAPTCHA div an ID and pass it to the
getResponse
method:e.g.
Access response:
or
我在页脚中有联系表单,始终显示,并且某些页面(例如创建帐户)也可以有验证码,因此它是动态的,我正在使用 jQuery 的下一种方式:
html:
javascript
I have contact form in footer that always displays and also some pages, like Create Account, can have captcha too, so it's dynamically and I'm using next way with jQuery:
html:
javascript
这是 raphadko 和 noun 提供的答案的无 JQuery 版本。
1) 通常使用以下命令创建验证码字段:
2) 使用以下命令加载脚本:
3) 现在调用它来迭代字段并创建验证码:
This is a JQuery-free version of the answer provided by raphadko and noun.
1) Create your recaptcha fields normally with this:
2) Load the script with this:
3) Now call this to iterate over the fields and create the recaptchas:
查看页面的源代码,我获取了 reCaptcha 部分并对代码进行了一些更改。 这是代码:
HTML:
jQuery:
我在这里使用简单的 javascript 选项卡功能。 所以,没有包含该代码。
当用户点击“请求信息”
(#product_tabs_what)
时,JS 将检查recapExist
是否为false
或是否具有某些值。 如果它有一个值,那么这将调用Recaptcha.destroy();
来销毁旧加载的 reCaptcha 并为此选项卡重新创建它。 否则,这只会创建一个 reCaptcha 并将其放入#more_info_recaptcha_box
div 中。 与“报价”#product_tabs_wha
选项卡相同。Looking at the source code of the page I took the reCaptcha part and changed the code a bit. Here's the code:
HTML:
jQuery:
I am using here simple javascript tab functionality. So, didn't included that code.
When user would click on "Request Information"
(#product_tabs_what)
then JS will check ifrecapExist
isfalse
or has some value. If it has a value then this will callRecaptcha.destroy();
to destroy the old loaded reCaptcha and will recreate it for this tab. Otherwise this will just create a reCaptcha and will place into the#more_info_recaptcha_box
div. Same as for "Make Offer"#product_tabs_wha
tab.要在 raphadko 的答案中添加一点:因为您有多个验证码(在一页上) ),您不能使用(通用)
g-recaptcha-response
POST 参数(因为它只包含一个验证码的响应)。 相反,您应该对每个验证码使用grecaptcha.getResponse(opt_widget_id)
调用。 这是我的代码(假设每个验证码都在其表单内):HTML:
和
JavaScript:
请注意,我应用了事件委托(请参阅 在追加元素后刷新 DOM )到所有动态修改的元素。 这将每个单独的验证码的响应绑定到其表单
submit
事件。To add a bit to raphadko's answer: since you have multiple captchas (on one page), you can't use the (universal)
g-recaptcha-response
POST parameter (because it holds only one captcha's response). Instead, you should usegrecaptcha.getResponse(opt_widget_id)
call for each captcha. Here's my code (provided each captcha is inside its form):HTML:
and
JavaScript:
Notice that I apply the event delegation (see refresh DOM after append element ) to all the dynamically modified elements. This binds every individual captha's response to its form
submit
event.一个不错的选择是即时为每个表单生成验证码输入(我已经用两个表单完成了,但您可能可以使用三个或更多表单)。 我使用 jQuery、jQuery 验证和 jQuery 表单插件通过 AJAX 以及 Recaptcha AJAX API 发布表单 -
https://developers.google.com/recaptcha/docs/display#recaptcha_methods
当用户提交其中一种表单时:
然后,他们可以填写验证码并重新提交表单。 如果他们决定提交不同的表单,那么您的代码会检查现有的验证码,因此页面上一次只有一个验证码。
A good option is to generate a recaptcha input for each form on the fly (I've done it with two but you could probably do three or more forms). I'm using jQuery, jQuery validation, and jQuery form plugin to post the form via AJAX, along with the Recaptcha AJAX API -
https://developers.google.com/recaptcha/docs/display#recaptcha_methods
When the user submits one of the forms:
Then, they can fill out the recaptcha and re-submit the form. If they decide to submit a different form instead, well, your code checks for existing recaptchas so you'll only have one recaptcha on the page at a time.
这是一个基于许多优秀答案的解决方案。 此选项是 jQuery 自由且动态的,不需要您通过 id 专门定位元素。
1) 像平常一样添加 reCAPTCHA 标记:
2) 将以下内容添加到文档中。 它将在任何支持 querySelectorAll API 的浏览器中运行
Here's a solution that builds off many of the excellent answers. This option is jQuery free, and dynamic, not requiring you to specifically target elements by id.
1) Add your reCAPTCHA markup as you normally would:
2) Add the following into the document. It will work in any browser that supports the querySelectorAll API
我会使用隐形验证码。 然后在按钮上使用“ formname='yourformname' ”之类的标签来指定要提交的表单并隐藏提交表单输入。
这样做的好处是它允许您保持 html5 表单验证完整,一个 recapcha,但多个按钮界面。 只需捕获由 recaptcha 生成的令牌密钥的“captcha”输入值。
我发现这更简单,更容易管理。
I would use invisible recaptcha. Then on your button use a tag like " formname='yourformname' " to specify which form is to be submitted and hide a submit form input.
The advantage of this is it allows for you to keep the html5 form validation intact, one recaptcha, but multiple button interfaces. Just capture the "captcha" input value for the token key generated by recaptcha.
I find this FAR simpler and easier to manage.
这是可能的,只需覆盖 Recaptcha Ajax 回调即可。 工作jsfiddle: http://jsfiddle.net/Vanit/Qu6kn/
你甚至不需要proxy div 因为覆盖后 DOM 代码将不会执行。 每当您想再次触发回调时,请调用 Recaptcha.reload()。
It is possible, just overwrite the Recaptcha Ajax callbacks. Working jsfiddle: http://jsfiddle.net/Vanit/Qu6kn/
You don't even need a proxy div because with the overwrites the DOM code won't execute. Call Recaptcha.reload() whenever you want to trigger the callbacks again.
这是一个很好的指南,可以做到这一点:
http://mycodde.blogspot.com.ar/2014/12/multiple-recaptcha-demo-same-page.html
基本上,您向 api 调用添加一些参数并手动渲染每个 recaptcha:
PS:“ grecaptcha.render”方法接收 ID
Here is a nice guide for doing exactly that:
http://mycodde.blogspot.com.ar/2014/12/multiple-recaptcha-demo-same-page.html
Basically you add some parameters to the api call and manually render each recaptcha:
PS: The "grecaptcha.render" method receives an ID
还需要包含这个脚本,仅此而已!
also need to include this script and that's it!
使用当前版本的 Recaptcha (reCAPTCHA API 版本 2.0),您一页上可以有多个验证码。
无需克隆 Recaptcha,也无需尝试解决该问题。 您只需为 Recaptcha 放置多个
元素,并在其中显式渲染 Recaptcha。
使用 Google Recaptcha API 即可轻松实现这一点。 下面是 HTML 代码示例:
在 Javascript 代码中,您必须为 Recaptcha 定义一个回调函数:
之后,您的 Recaptcha 脚本 URL 应如下所示:
或者,您可以提供一个类名,而不是向您的 Recaptcha 字段提供 ID并使用类选择器循环这些元素并调用
.render()
。With the current version of Recaptcha (reCAPTCHA API version 2.0), you can have multiple Recaptchas on one page.
There is no need to clone the Recaptcha nor try to workaround the problem. You just have to put multiple
<div>
elements for the Recaptchas and render the Recaptchas inside them explicitly.This is easy with the Google Recaptcha API. Here is the example HTML code:
In your Javascript code, you have to define a callback function for Recaptcha:
After this, your Recaptcha script URL should look like this:
Or instead of giving IDs to your Recaptcha fields, you can give a class name and loop these elements with your class selector and call
.render()
.这可以通过 jQuery 的
clone()
函数轻松完成。因此,您必须为验证码创建两个包装 div。 我的第一个表单的 recaptcha div:
第二个表单的 div 为空(不同的 ID)。 所以我的只是:
那么javascript非常简单:
可能不需要在
clone()
中使用带有true
值的第二个参数,但拥有它也没有什么坏处它...此方法的唯一问题是,如果您通过 ajax 提交表单,问题是您有两个具有相同名称的元素,并且您必须更聪明地捕获正确元素值的方式(reCaptcha 元素的两个 id 是#recaptcha_response_field
和 #recaptcha_challenge_field,以防万一有人需要它们)This is easily accomplished with jQuery's
clone()
function.So you must create two wrapper divs for the recaptcha. My first form's recaptcha div:
The second form's div is empty (different ID). So mine is just:
Then the javascript is quite simple:
Probably don't need the second parameter with a
true
value inclone()
, but doesn't hurt to have it... The only issue with this method is if you are submitting your form via ajax, the problem is that you have two elements that have the same name and you must me a bit more clever with the way you capture that correct element's values (the two ids for reCaptcha elements are#recaptcha_response_field
and #recaptcha_challenge_field just in case someone needs them)此答案是@raphadko的答案的扩展。
如果您需要手动提取验证码代码(例如在ajax请求中),您必须调用:
但是如何检索小部件id参数?
我使用CaptchaCallback的定义存储每个 g-recaptcha 框的小部件 ID(作为 HTML 数据属性):
然后我可以调用:
来提取代码。
This answer is an extension to @raphadko's answer.
If you need to extract manually the captcha code (like in ajax requests) you have to call:
But how can you retrieve the widget id parameter?
I use this definition of CaptchaCallback to store the widget id of each g-recaptcha box (as an HTML data attribute):
Then I can call:
to extract the code.
关于在 ASP 页面上执行此操作,有人提出了类似的问题(链接 )并且那里的共识是不可能使用 recapcha 来完成。 似乎单个页面上的多个表单必须共享验证码,除非您愿意使用不同的验证码。 如果您没有被 recaptcha 锁定,那么可以看看 Zend Frameworks Zend_Captcha 组件 (链接)。 它包含一些
A similar question was asked about doing this on an ASP page (link) and the consensus over there was that it was not possible to do with recaptcha. It seems that multiple forms on a single page must share the captcha, unless you're willing to use a different captcha. If you are not locked into recaptcha a good library to take a look at is the Zend Frameworks Zend_Captcha component (link). It contains a few
简单明了:
通常使用以下命令创建您的验证码字段:
使用以下内容加载脚本:
现在调用它来迭代字段并创建验证码:
Simple and straightforward:
Create your Recaptcha fields normally with this:
Load the script with this:
Now call this to iterate over the fields and create the Recaptchas: