3

我有一个带有Bootstrap 验证器的注册表单。我想在提交表单之前验证 google INVISIBLE reCaptcha(客户端)。有一些例子(在客户端验证后调用不可见的 reCAPTCHA 挑战)。我试图改变这部分:

  function validate(event) {
    event.preventDefault();
    $('#form-reg').validator('validate').on('submit', function (e) {
        if (e.isDefaultPrevented()) {
            //...
        } else {
            grecaptcha.execute();
        };
    });
  };

这不起作用,我不知道这是否是正确的方法。请建议如何加入隐形 reCaptcha 和 Bootstrap 验证器。感谢您的帮助。

4

1 回答 1

3

下面的代码对我有用

<?php
$config = require('config.php');
?>
<html>
  <head>
    <title>reCAPTCHA demo</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <!-- Boostrap Validator --> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.9/validator.min.js" ></script>

    <script type="text/javascript">
    $(document).ready(function(){
        $('#demo-form').validator().on('submit', function (e) {
          if (e.isDefaultPrevented()) {
            // handle the invalid form...
            console.log("validation failed");
          } else {
            // everything looks good!
            e.preventDefault();
            console.log("validation success");
            grecaptcha.execute();
          }
        });
    }); 

    function onSubmit(token){
        document.getElementById("demo-form").submit();
    };

    </script>

  </head>
  <body>
    <div class="container">
    <br>
        <div class="row">
            <div class="col-md-5 col-md-offset-3">
                <form id="demo-form" data-toggle="validator" role="form"  action="admin.php" method="POST" >
                    <div class="form-group">
                        <label for="inputName" class="control-label">Name</label>
                        <input type="text" class="form-control" id="inputName" placeholder="Geordy James" required/>
                    </div>
                    <div id='recaptcha' class="g-recaptcha"
                          data-sitekey="<?php echo $config['client-key']; ?>"
                          data-callback="onSubmit"
                          data-size="invisible"></div>
                    <button class="btn btn-block btn-primary"  type="submit">Submit</button>
                </form>
            </div>
        </div>
    </div>
    <script src="https://www.google.com/recaptcha/api.js" async defer ></script>
  </body>
</html>

我在这个程序中使用了非官方的 Google Invisible reCAPTCHA PHP 库,您可以从https://github.com/geordyjames/google-Invisible-reCAPTCHA下载它。如果此方法不适合您,请在下方评论。

于 2017-04-19T15:24:21.813 回答