【问题标题】:How to jQuery Validate the ReCaptcha?如何 jQuery 验证 ReCaptcha?
【发布时间】:2016-07-23 14:01:27
【问题描述】:

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>

ZF2 的 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
 }

是否可以像这样远程验证它:https://jqueryvalidation.org/remote-method

我在下面的远程 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);
}

而js本身就是:

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

有可能还是我做错了什么?

【问题讨论】:

  • 根据 jQuery 文档,不推荐使用 $().ready(function()。请改用$(document).ready(function()。另外,你做了什么故障排除? json.php 是否在正确的位置并被调用?您是否正在使用控制台检查 Ajax 调用?此外,除非您使用remotedata 选项,否则您只会将recaptcha_response_field 的值发送到您的PHP 脚本。
  • 由于相同表单的其他字段使用相同的 json.php 进行远程验证,我假设它被调用。我会试试你的版本并通过控制台检查。
  • 如何使用数据选项以及如何发送recaptcha_challlenge_field? @Sparky
  • 还要详细描述“不起作用”
  • 查看 jQuery Validate 文档中的示例,该示例展示了如何使用 data 选项。

标签: php jquery jquery-validate recaptcha


【解决方案1】:

试试这个函数来验证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;
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 2015-03-10
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多