Google INVISIBLE reCaptcha + Bootstrap 验证器

2024-03-13

我有一张登记表引导验证器 http://1000hz.github.io/bootstrap-validator/。我想在提交表单之前验证 google INVISIBLE reCaptcha(客户端)。 有一些exapmle https://developers.google.com/recaptcha/docs/invisible#example(在客户端验证后调用不可见的 reCAPTCHA 质询)。 我尝试改变这部分:

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

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


下面的代码对我有用

<?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 https://github.com/geordyjames/google-Invisible-reCAPTCHA。 如果此方法不适合您,请在下面评论。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google INVISIBLE reCaptcha + Bootstrap 验证器 的相关文章

随机推荐