sg5*_*552 1 jquery jquery-validate recaptcha
我使用新的reCaptcha和jQuery Validate插件.验证插件适用于我的表单但不幸的是它不适用于reCaptcha.我试图让它在下面工作:
HTML:
<div class="g-recaptcha" name="recaptcha" id="recaptcha" data-sitekey="XXXXX"></div>
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
recaptcha: {
required: true
}
Run Code Online (Sandbox Code Playgroud)
但它没有奏效.有谁知道如何解决这一问题.我只需要默认的这个字段是必需的错误信息.
你应该为reCaptcha添加隐藏的输入,因为jQuery Validation只能验证input/textarea/select:
<div class="g-recaptcha" data-sitekey="XXXXX"></div>
<input type="hidden" name="recaptcha" data-rule-recaptcha="true">
<!--
We set "data-rule-recaptcha" — this will require jQuery Validate
to validate this field using method named "recaptcha"
(which we write below)
-->
Run Code Online (Sandbox Code Playgroud)
然后像这样重写你的JS:
// By default jQuery Validation ignore hidden inputs,
// so we change settings to ignore inputs with class "no-validation".
// Cause our hidden input didn't have this class — it would be validated!
$('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");
Run Code Online (Sandbox Code Playgroud)
就这样.
| 归档时间: |
|
| 查看次数: |
9567 次 |
| 最近记录: |