有没有人知道如何使"谷歌ReCAPTCHA (v2)“成为form
中的”必需“
我的意思是在填好recaptcha之前不提交表格?
我在我的表单中使用ParsleyJs,但是没有找到一种方法使它与div
的.
发布于 2019-04-02 16:35:39
您可以在这里找到另一个有用的示例:https://codepen.io/reactionmedia/pen/JVdmbB
对于这个例子,我将在表单中创建两个HTML元素:
<div id="botvalidator"></div>
<div id="captchaerrors"></div>
僵尸验证器将包含google和“我不是机器人”复选框。在检查用户没有单击“我不是机器人”复选框后,captcha差错将包含错误。
重要:我们使用arrive.js库来了解google何时在DOM中添加一个新的g- recaptcha -响应textarea元素,因为以前的DOM新节点插入验证不再有效。此事件将在recaptcha加载到页面中几分钟后发生。
您可以从: arrive.js库下载https://github.com/uzairfarooq/arrive/
或者直接从CDN提供程序插入它,例如:https://cdnjs.cloudflare.com/ajax/libs/arrive/2.4.1/arrive.min.js
请记住在加载JQUERY库后插入所有库以避免错误。我正在使用jQuery2.2.4版本
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
另一件重要的事情是记住以这种方式加载recaptcha库,以便在加载recaptcha之后执行onloadCallback函数,并“手动”呈现recaptcha。
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"></script>
下面是代码片段:
var onloadCallback = function() {
/**
* If we try to load page to show the congrats message we don't need to load recaptcha.
*/
if($("#botvalidator").length > 0) {
grecaptcha.render('botvalidator', {
'sitekey' : '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI',
'callback': cleanErrors
});
addCaptchaValidation();
/**
* We are going to use arrive library in order to check new google recaptcha
* element added after the current one is expired and then we will create new attributes for that element.
* Expires-callback google recaptcha is not working, probably it is executed before element creation.
* https://github.com/uzairfarooq/arrive/
*/
$(document).arrive("#g-recaptcha-response", function() {
// 'this' refers to the newly created element
addCaptchaValidation();
});
}
};
/** We are going to remove all errors from the container. */
var cleanErrors = function() {
$("#captchaerrors").empty();
};
var addCaptchaValidation = function() {
console.log("Adding captcha validation");
cleanErrors();
$('#myform').parsley().destroy();
$('#g-recaptcha-response').attr('required', true);
// We are going to create a new validator on Parsley
$('#g-recaptcha-response').attr('data-parsley-captcha-validation', true);
$('#g-recaptcha-response').attr('data-parsley-error-message', "We know it, but we need you to confirm you are not a robot. Thanks.");
$('#g-recaptcha-response').attr('data-parsley-errors-container', "#captchaerrors");
$('#myform').parsley();
};
/** We are going to add a new Parsley validator, this validation is called from
#g-recaptcha-response after clicking the submit button*/
window.Parsley.addValidator('captchaValidation', {
validateString: function(value) {
if(debug) console.log("Validating captcha", value);
if(value.length > 0) {
return true;
} else {
return false;
}
},
messages: {en: 'We know it, but we need you to confirm you are not a robot. Thanks.'}
});
<html>
<head>
</head>
<body>
<h1>Parsley and Google Recaptcha Example</h1>
<form id="myform">
Full name
<input type="text" name="name" id="name" data-parsley-required="true">
<br/>
<div id="botvalidator"></div>
<div id="captchaerrors"></div><br/>
<input type="submit" value="Submit Form">
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/parsley.js/2.8.2/parsley.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/arrive/2.4.1/arrive.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"></script>
</body>
</html>
都是这样的人。
https://stackoverflow.com/questions/29612879
复制相似问题