[英]Require User to click google's new recaptcha before form submission
I am using google's new recaptcha inside my form (HTML5): https://www.google.com/recaptcha我在我的表单中使用谷歌的新 recaptcha (HTML5): https://www.google.com/recaptcha
Is there a way to check and mark recaptcha as required before form submission?有没有办法在提交表单之前根据需要检查和标记recaptcha? I want to validate this on client-side instead of server side.
我想在客户端而不是服务器端验证这一点。 That way, I don't have to go back to the form and warn the user about not entering anything for the captcha.
这样,我就不必 go 返回表单并警告用户不要为验证码输入任何内容。
Any javascript that I can use to check whether users enter anything in recaptcha?我可以使用任何 javascript 来检查用户是否在 recaptcha 中输入了任何内容?
You can check the textarea field with id g-recaptcha-response
.您可以使用 ID
g-recaptcha-response
检查 textarea 字段。 You can do as following:您可以执行以下操作:
$("form").submit(function(event) {
var recaptcha = $("#g-recaptcha-response").val();
if (recaptcha === "") {
event.preventDefault();
alert("Please check the recaptcha");
}
});
Hope this helps you.希望这对你有帮助。
A vanilla JavaScript implementation using parts of both Ankesh's and Lammert's solution:使用 Ankesh 和 Lammert 解决方案的一部分的普通 JavaScript 实现:
var form = document.getElementById('ctct-custom-form');
form.addEventListener("submit", function(event){
if (grecaptcha.getResponse() === '') {
event.preventDefault();
alert('Please check the recaptcha');
}
}
, false);
Credit to for form submit listener code: How can I listen to the form submit event in javascript?归功于表单提交侦听器代码: 如何在 javascript 中侦听表单提交事件?
grecaptcha.getResponse()
- Returns the response for the Recaptcha. grecaptcha.getResponse()
- 返回 Recaptcha 的响应。 You can check this in condition such as您可以在条件中检查此内容,例如
grecaptcha.getResponse(opt_widget_id) === ""
Optional widget ID, defaults to the first widget created if unspecified.可选的小部件 ID,如果未指定,则默认为创建的第一个小部件。
Ref: https://developers.google.com/recaptcha/docs/display参考: https : //developers.google.com/recaptcha/docs/display
I tried to improve rylanb's answer.我试图改进 rylanb 的回答。 The code below find all the forms in the page that have g-captcha activated and prevent from submission.
下面的代码查找页面中所有已激活 g-captcha 并阻止提交的表单。 Note: It assumes that your
div.g-recaptcha
is the child of form
注意:它假设你的
div.g-recaptcha
是form
的孩子
const forms = document.querySelectorAll('div.g-recaptcha');
forms.forEach(form=> {
const formParentElement = form.parentElement;
formParentElement.addEventListener("submit", e => {
if (grecaptcha.getResponse() === '') {
e.preventDefault();
alert('Please check the recaptcha');
}
}, false)
});
Working solution in 2022 2022年的工作解决方案
Personally, I was not able to get any of the above solutions to work with my captcha.就个人而言,我无法获得上述任何解决方案来使用我的验证码。 So I figured I would share my current working solution for those facing the same issue.
所以我想我会为那些面临同样问题的人分享我目前的工作解决方案。
My notes in the .js
should explain the solution thoroughly.我在
.js
中的笔记应该彻底解释解决方案。
JavaScript JavaScript
// By default do not allow form submission.
var allow_submit = false
function captcha_filled () {
/*
* This is called when Google get's the recaptcha response and approves it.
* Setting allow_submit = true will let the form POST as normal.
* */
allow_submit = true
}
function captcha_expired () {
/*
* This is called when Google determines too much time has passed and expires the approval.
* Setting allow_submit = false will prevent the form from being submitted.
* */
allow_submit = false
}
function check_captcha_filled (e) {
console.log('verify-captcha')
/*
* This will be called when the form is submitted.
* If Google determines the captcha is OK, it will have
* called captcha_filled which sets allow_submit = true
* If the captcha has not been filled out, allow_submit
* will still be false.
* We check allow_submit and prevent the form from being submitted
* if the value of allow_submit is false.
* */
// If captcha_filled has not been called, allow_submit will be false.
// In this case, we want to prevent the form from being submitted.
if (!allow_submit) {
// This call prevents the form submission.
// e.preventDefault()
// This alert is temporary - you should replace it with whatever you want
// to do if the captcha has not been filled out.
alert('ERROR: Please verify you are human by filling out the captcha')
return false
}
captcha_expired()
return true
}
HTML HTML
<form action="post" onsubmit="return check_captcha_filled()">
<!-- form items -->
<div class="g-recaptcha"
data-callback="captcha_filled"
data-expired-callback="captcha_expired"
data-sitekey="your site key">
</div>
</form>
Easiest way to do this is by changing type of button.最简单的方法是更改按钮类型。
<button class="theme-btn" type="button" onclick="checkForm()" id="sign_up_button" >
now create a Java Script Function to change the type of submit on validation.现在创建一个 Java 脚本函数来更改验证时提交的类型。
<script type="text/javascript">
// NoCaptcha Function // 无验证码函数
function checkForm(){
if(!document.getElementById("g-recaptcha-response").value){
alert("Please Prove ! You're not a Robot");
return false;
}else{
document.getElementById("sign_up_button").type = "submit";
return true;
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.