0

I have a form where I am using Jquery validate for client side validations. I have also used a google recaptcha in the form. The problem here is, I want to run client side validations on the reCaptcha div so that the user does not leave it unchecked. I have a function which validates the recaptcha but I am not able to run it simultaneously with the validate. I have to explicitly mention it in my submitHandler of the validate. Below is my code snippet:

$('#newform').validate({
        'rules': {
            'name': {
                required: true
            },
            'age': {
                required: true,
                number: true
            }
        },
        'messages': {
            'name': {
                required: 'Name is required'
            },
            'age': {
                required: 'Age is required',
                number: 'Age must be a number'
            }
        },
        'submitHandler': function(form) {
            recaptcha_validate(form); //This runs the validation on the reCaptcha
        }
    });

<form id="newform" enctype="multipart/form-data" method="post" action="">
        <input type="text" name="name" placeholder="name"></input><br/>
        <input type="text" name="age" placeholder="age"></input><br/>
        <input type="file" id="fileupload" name="file"/><br/>
        <div class="g-recaptcha" data-sitekey="site-key"></div>
        <span id="captcha_error"/> <!-- Displays the error message for captcha -->
        <input type="submit" name="send" value="Send"></input>
</form>

My question: Is it possible to run the captcha validation function at the same time as the actual validation handlers run? Currently the captcha validation runs after all the fields are validated, since it is written in the submitHandler.

reCaptcha validation function courtesy THIS post.

Community
  • 1
  • 1
Aditya
  • 1,241
  • 5
  • 19
  • 29
  • Please show the actual code *you are using* rather than linking to another question. You might be able to do it using a custom rule. See `.addMethod()`. – Sparky May 19 '16 at 15:49
  • Well @Sparky this is the actual code. The link to another question is just a reference to the function I'm using which is not really relevant to the problem here since it is working just fine. Also `.addMethod()` is less likely to work since reCaptcha is an external API. Anyways which I shall try to use it for sure. – Aditya May 20 '16 at 03:58
  • Since you're asking how to integrate this functionality into jQuery Validate, of course it's relevant to show the code that is already working. Regardless, if you want a rule that is part of the plugin and evaluated at the same time as the other rules, then `.addMethod()` is the way to attempt this. – Sparky May 20 '16 at 15:50

0 Answers0