我在aspx中有一个简单的联系人表单。我想在提交表单之前验证reCaptcha (客户端)。请帮帮忙。
示例代码:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test Form</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
$("#cmdSubmit").click(function () {
//need to validate the captcha
});
</script>
</head>
<body>
<form id="form1" runat="server">
<label class="clsLabe">First Name<sup>*</sup></label><br />
<input type="text" id="txtFName" name="txtFName" class="clsInput" /><br />
<div class="g-recaptcha" data-sitekey="my_key"></div>
<img id="cmdSubmit" src="SubmitBtn.png" alt="Submit Form" style="cursor:pointer;" />
</form>
</body>
</html>
我想在cmdSubmit
点击上验证验证码。
请帮帮忙。
发布于 2015-02-19 21:42:47
使用这个来用简单的javascript验证google captcha。
html主体中的以下代码:
<div class="g-recaptcha" id="rcaptcha" style="margin-left: 90px;" data-sitekey="my_key"></div>
<span id="captcha" style="margin-left:100px;color:red" />
此代码放在call get_action( This )方法表单按钮的标题部分:
function get_action(form)
{
var v = grecaptcha.getResponse();
if(v.length == 0)
{
document.getElementById('captcha').innerHTML="You can't leave Captcha Code empty";
return false;
}
else
{
document.getElementById('captcha').innerHTML="Captcha completed";
return true;
}
}
发布于 2015-01-20 19:49:33
如果在回调中呈现Recaptcha
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>
使用空DIV作为占位符
<div id='html_element'></div>
然后,您可以为成功的CAPTCHA响应指定一个可选的函数调用
var onloadCallback = function() {
grecaptcha.render('html_element', {
'sitekey' : 'your_site_key',
'callback' : correctCaptcha
});
};
然后,recaptcha响应将被发送到'correctCaptcha‘函数。
var correctCaptcha = function(response) {
alert(response);
};
所有这些都来自Google API注释:
我有点不确定你为什么要这么做。通常,您会将g-recaptcha-response字段与您的私钥一起发送,以安全地验证服务器端。除非您想禁用提交按钮,直到recaptcha成功或类似的情况-在这种情况下,上面应该可以工作。
希望这能有所帮助。
保罗
发布于 2016-03-10 03:33:25
简化了Paul的回答:
来源:
<script src="https://www.google.com/recaptcha/api.js"></script>
HTML:
<div class="g-recaptcha" data-sitekey="YOUR_KEY" data-callback="correctCaptcha"></div>
JS:
var correctCaptcha = function(response) {
alert(response);
};
https://stackoverflow.com/questions/27902539
复制相似问题