Google INVISIBLE reCaptcha + Bootstrap validator

2019-05-15 01:54发布

问题:

I have a registration form with Bootstrap validator. I want to validate the google INVISIBLE reCaptcha (client-side) before submitting the form. There is some exapmle (Invoking the invisible reCAPTCHA challenge after client side validation). I tried to change this part:

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

This does not work and I dont know if it's the right way. Please advice how to join invisible reCaptcha and Bootstrap validator. Thank you for your help.

回答1:

The below code work for me

<?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>

I used Unofficial Google Invisible reCAPTCHA PHP library in this program and you can download it from https://github.com/geordyjames/google-Invisible-reCAPTCHA . If this method doesn't work for you please comment below.