简体   繁体   English

提交表单需要Google Recaptcha

[英]Google recaptcha required for form submission

I need help in order to validate our form to be submitted via google recaptcha, I have successfully implemented the code however the form still allows submission without the recaptcha, how do I make it required field? 我需要帮助才能验证我们要通过Google Recaptcha提交的表单,我已经成功实现了代码,但是该表单仍然允许提交没有Recaptcha的表单,我如何将其设置为必填字段? not very good with java script here, any assistance is greatly appreciated also if an error message would pop up so that asking people to complete recaptcha that would be great, thanks a lot 这里的Java脚本不是很好,如果弹出错误消息,以便要求人们完成重新输入,那将是很大的,非常感谢您的帮助,非常感谢

CSS CSS

<script src='https://www.google.com/recaptcha/api.js'></script>

Html HTML

<div class="form">

%form_errors%

<div align="right"><p><strong>* Denotes mandatory field</strong></p></div>


<fieldset>

<legend>Your enquiry details</legend>

<div class="form-row">
%question_label_746942_q7%
%question_field_746942_q7%
</div>

</fieldset>

<div class="g-recaptcha" data-sitekey="google site key"></div>
<div class="form-row-submit" align="right">

%submit_button%
</div>

</div>

You can by default set the submit button to disabled and only enable it on the callback function from reCAPTCHA. 您可以默认将提交按钮设置为禁用,并且仅在reCAPTCHA的回调函数上启用它。

Check this: https://developers.google.com/recaptcha/docs/display 检查此: https : //developers.google.com/recaptcha/docs/display

Make sure to validate the form on the server side too. 确保在服务器端也验证表单。

UPDATE UPDATE

Add this in your : 将此添加到您的:

<script>
    function enableBtn(){
        document.getElementById("submit_details").disabled = false;
    }
</script>

And then call the above function in the reCAPTCHA div in your form as follows: 然后按以下形式在reCAPTCHA div中调用上述函数:

<div class="g-recaptcha" data-sitekey="YOUR_KEY" data-callback="enableBtn"></div>
<button type="submit" id="submit_details" disabled>Send</button>

This will have the submit button disabled when the page loads so the user won't be able to submit the form. 当页面加载时,这将禁用提交按钮,因此用户将无法提交表单。 The button is enabled only after the user passes the "I'm not a robot" test. 仅在用户通过“我不是机器人”测试后,该按钮才能启用。

Again, remember to validate the form data on the server side. 同样,请记住在服务器端验证表单数据。

It should be <button type="submit" id="submit_details" disabled>Send</button> 应该<button type="submit" id="submit_details" disabled>Send</button>

Not if="submit_details" 不是if="submit_details"

The function is using getElementById("submit_details") , so it is looking for an ID of "submit_details" in order to remove the disabled condition of the button by setting disabled = false instead of disabled = true which is the default condition that is set inline in the <button> tag. 该函数正在使用getElementById("submit_details") ,因此它正在寻找“ submit_details”的ID,以便通过设置disabled = false而不是disabled = true来删除按钮的disabled = false条件,这是设置的默认条件内联在<button>标记中。

<form action="" method="post" onsubmit="return validateRecaptcha();">
</form>

<script>
function validateRecaptcha() {
    var response = grecaptcha.getResponse();
    if (response.length === 0) {
        alert("You need to fill the captcha");
        return false;
    } else {
        alert("validated");
        return true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM