3

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
  • Haha, I had the exactly same issue, your code was identical to mine. The answer below works!
    – Maurice
    Apr 27, 2017 at 7:47

1 Answer 1

3

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.

1
  • 1
    THANK YOU!!! I spent way too long trying to figure this out. This worked perfectly.
    – Maurice
    Apr 27, 2017 at 7:46

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.