无论有效/无效,都会在reCAPTCHA上显示jQuery验证错误消息

时间:2017-01-04 03:54:12

标签: jquery jquery-validate recaptcha

我正在尝试将jQuery验证脚本与新的ReCAPTCHA一起使用,但ReCAPTCHA无法验证,即使它应该是有效的。这是我的代码的简化版本:

<body>
    <form id="contact" method="post" action="formmail.php">
        <input type="hidden" id="recipients" name="recipients" value="krishunt@toprival.com" />
        <input type="hidden" name="required" value="realname: Your name" />
        <input type="hidden" name="derive_fields" value="imgverify=g-recaptcha-response" />

        <p><label for="realname">Name</label><br/>
        <input type="text" name="realname" id="realname" class="required"/></p>

        <label>Are you a robot?</label><br/>
        <input type="hidden" class="hiddenRecaptcha required" name="hiddenRecaptcha" id="hiddenRecaptcha">
        <div class="g-recaptcha" data-sitekey="6LewiRAUAAAAAF9928pTj2nOoKYyDKA3WpxCoit4" data-callback="recaptchaCallback"></div>
        <label class="error" for="hiddenRecaptcha" generated="true"></label>

        <p style="margin-top:20px"><input type="submit" value="Submit"></p>
    </form>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script>
    <script src="jquery.validate.js" type="text/javascript"></script>
    <script src='https://www.google.com/recaptcha/api.js'></script>

    <script>
    jQuery(document).ready(function() {

        function recaptchaCallback() {
            jQuery('#hiddenRecaptcha').valid();
        };

        jQuery("#contact").validate({
            ignore: ".ignore",
            messages: {
                realname: "Please enter your full name.",
                hiddenRecaptcha: "Please confirm that you are not a robot."
            },
            hiddenRecaptcha: {
                required: function() {
                    if (grecaptcha.getResponse() == '') {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        });

    });
    </script>
</body>

Here是该页面的实时版本。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

我认为hiddenRecaptcha规则需要进入规则数组。

e.g。

jQuery("#contact").validate({
        ignore: ".ignore",
        messages: {
            realname: "Please enter your full name.",
            hiddenRecaptcha: "Please confirm that you are not a robot."
        },
        rules: {
            "hiddenRecaptcha": {
                required: function() {
                    if (grecaptcha.getResponse() == '') {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        }
     });