简体   繁体   English

jQuery如何验证ReCaptcha?

[英]How to jQuery Validate the ReCaptcha?

Recaptcha form is like this: Recaptcha形式如下:

<script type="text/javascript"> var RecaptchaOptions = {"theme":"red","lang":"en"}; </script><script type="text/javascript" src="https://www.google.com/recaptcha/api/challenge?k=6LeThAsTAAAAAKYRjSpA8XZ1s4izK65hYr9ulCiD"> </script><noscript> <iframe src="https://www.google.com/recaptcha/api/noscript?k=6LeThAsTAAAAAKYRjSpA8XZ1s4izK65hYr9ulCiD" height="300" width="500" frameborder="0"> </iframe><br> <textarea name="recaptcha_challenge_field" rows="3" cols="40"> </textarea> <input type="hidden" name="recaptcha_response_field" value="manual_challenge"> </noscript>

and validator of ZF2 for ReCaptcha is like this: ZF2 for ReCaptcha的验证器是这样的:

 $recaptcha = new ZendService\ReCaptcha\ReCaptcha(PUB_KEY, PRIV_KEY);
 $html = $recaptcha->getHTML();
 $result = $recaptcha->verify($_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
 if (!$result->isValid()) {
  // invalid
 } else {
 // valid
 }

is it possible to validate it remotely like this: https://jqueryvalidation.org/remote-method 是否可以像这样远程验证: https : //jqueryvalidation.org/remote-method

I tried below in remote php file and it doesn't work: 我在远程php文件中尝试以下操作,但它不起作用:

$recaptcha = new ZendService\ReCaptcha\ReCaptcha(PUB_KEY, PRIV_KEY);
$result = $recaptcha->verify($_GET['recaptcha_challenge_field'], $_GET['recaptcha_response_field']);
if (!$result->isValid()) {
echo json_encode(false);
} else {
echo json_encode(true);
}

and js itself is: js本身是:

$().ready(function() {
$("#contact").validate({
                      rules: {
                               recaptcha_response_field: {
                                          required: true,
                                          remote: "json.php"
                               }             
                  }   
 });
 });

is it possible at all or I did something wrong? 有可能吗,或者我做错了什么?

try this function to validate recaptcha 试试这个功能来验证recaptcha

var grecaptchaId;
        var onloadCallback = function () {
            grecaptchaId = grecaptcha.render('grecaptcha', {
                'sitekey': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
                'callback': function (response) {
                    $("#grecaptcha_error").text('');
                }
            });
        };

        function ValidateRecaptcha() {
            var x;
            x = grecaptcha.getResponse(grecaptchaId);
            if (x != "") {
                $("#grecaptcha_error").text('');
                return true;
            }
            else {
                $("#grecaptcha_error").text('The captcha is required and can\'t be empty');
                return false;
            }
        }

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

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