reCaptcha字段为必填项 [英] reCaptcha field as required
本文介绍了reCaptcha字段为必填项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在form
中使用recaptcha
,但是,我很难将其设置为required
.我尝试使用此问题的前两个答案的代码,但是,没有一个他们工作.
I am trying to use recaptcha
in a form
, however, I am having trouble to set it as required
. I tried to use the code of the first two answers of this question, however, none of them worked.
这是我的header
代码:
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js"></script>
这是我的body
代码(我首先尝试了已接受的问题):
Here is my body
code (I tried first the accepted question):
<form action="some-page.php">
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<input type="text" name="firstname" value="Name" required><br> Last name:<br>
<input type="text" name="lastname" value="Last Name" required><br><br>
<div class="g-recaptcha" data-sitekey="xxxx"></div>
<input type="hidden" name="recaptcha" data-rule-recaptcha="true">
<input type="submit" value="Submit">
</fieldset>
</form>
<script>
$('form').validate({
ignore: '.no-validation'
});
// Add our validation method for reCaptcha:
$.validator.addMethod("recaptcha", function(value, element) {
var grecaptcha = window.grecaptcha || null;
return !grecaptcha || grecaptcha.getResponse();
}, "Please fill reCAPTCHA");
</script>
推荐答案
function validateForm() {
var recaptcha = document.forms["myForm"]["g-recaptcha-response"].value;
if (recaptcha == "") {
alert("Please fill reCAPTCHA");
return false;
}
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js"></script>
<form name="myForm" action="login.php" onsubmit="return validateForm()" method="post">
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<input type="text" name="firstname" value="Name" required><br> Last name:<br>
<input type="text" name="lastname" value="Last Name" required><br><br>
<div class="g-recaptcha" data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"></div>
<input type="hidden" name="recaptcha" data-rule-recaptcha="true">
<input type="submit" value="Submit">
</fieldset>
</form>
这篇关于reCaptcha字段为必填项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文