jQuery验证插件为Google Recaptcha V.2添加了一条规则



我有一个使用Google Recaptcha V.2的注册页面。我想添加一个使用jQuery验证插件输入Recaptcha的规则。在注册页面中,以下代码用于显示Google Recaptcha:

<?php if(!$this->K2Params->get('recaptchaV2')): ?>
<label class="formRecaptcha"><?php echo JText::_('K2_ENTER_THE_TWO_WORDS_YOU_SEE_BELOW'); ?></label>
<?php endif; ?>
<div id="recaptcha" name="recaptcha" class="<?php echo $this->recaptchaClass; ?>"></div>

在rigister.js中,我添加了以下代码:

jQuery(($)=>{
$().ready(()=>{
// validate signup form on keyup and submit
$("#josForm").validate({
ignore: ".ignore",
rules: {
name: {
required: true,
minlength: 3
},
password: {
required: true,
minlength: 7
},
email: {
required: true,
email: true
},
},
messages: {
name: {
required: "enter your name",
minlength: "no less than 3 symbols"
},
password: {
required: "enter the password",
minlength: "no less than 7 symbols"
},
email: "enter your email",
email: {
required: "enter your email"
},
},
submitHandler: function(form) {
if (recaptcha.getResponse()) {
form.submit();
} else {
alert('Please confirm captcha to proceed')
}
},
});
});
});

但这条规则并不适用于Google Recaptcha。你能帮我们解决这个问题吗?

哦,我添加了hiddenRecaptcha的规则,并添加了带有id和name的隐藏输入"hiddenRecaptcha";。正确的代码如下:

<?php if(!$this->K2Params->get('recaptchaV2')): ?>
<label class="formRecaptcha"><?php echo JText::_('K2_ENTER_THE_TWO_WORDS_YOU_SEE_BELOW'); ?></label>
<?php endif; ?>
<div id="recaptcha" name="recaptcha" class="<?php echo $this->recaptchaClass; ?>"></div>
<input type="hidden" class="hiddenRecaptcha required" name="hiddenRecaptcha" id="hiddenRecaptcha" />

在register.js:中

jQuery(($)=>{
$().ready(()=>{
$("#josForm").validate({
ignore: ".ignore",
rules: {
name: {
required: true,
minlength: 3
},
password: {
required: true,
minlength: 7
},
email: {
required: true,
email: true
},
hiddenRecaptcha: {
required: function() {
if(grecaptcha.getResponse() == '') {
return true;
} else {
return false;
}
}
},
},
messages: {
name: {
required: "enter your name",
minlength: "no less than 3 symbols"
},
password: {
required: "enter the password",
minlength: "no less than 7 symbols"
},
email: "enter your email",
email: {
required: "enter your email"
},
},
});
});
});

现在这条规则适用于Captcha。

最新更新