Set reCaptcha field as required

北城以北 提交于 2019-12-08 04:14:05

问题


I use the new reCaptcha with the jQuery Validate plugin. The validation plugin works nice with my form but unfortunately it didn't work with reCaptcha. My attempt to make it work below:

HTML:

<div class="g-recaptcha" name="recaptcha" id="recaptcha" data-sitekey="XXXXX"></div> 

Javascript:

 recaptcha: {
    required: true
      }    

but it didn't worked. Does anyone know how to fix this. I simply need the default This field is required error message.


回答1:


You should add hidden input for reCaptcha, cause jQuery Validation can only validate input/textarea/select:

<div class="g-recaptcha" data-sitekey="XXXXX"></div>
<input type="hidden" name="recaptcha" data-rule-recaptcha="true">
<!-- 
     We set "data-rule-recaptcha" — this will require jQuery Validate
     to validate this field using method named "recaptcha"
     (which we write below) 
-->

Then rewrite your JS like this:

// By default jQuery Validation ignore hidden inputs, 
// so we change settings to ignore inputs with class "no-validation". 
// Cause our hidden input didn't have this class — it would be validated!
$('form').validate({
  ignore: '.no-validation'
});     

// Add our validation method for reCaptcha: 
$.validator.addMethod("recaptcha", function(value, element) {
  var grecaptcha = window.grecaptcha || null;
  return !grecaptcha || grecaptcha.getResponse();
}, "Please fill reCAPTCHA");

That's all.




回答2:


So I managed to make it worked. The only problem is after user completing reCaptcha, the message This field is required does not quickly dissapear. I'm using blur, keydown and focusout to detect user input and remove the error message.

JS:

$("#myForm").bind('blur keydown focusout', function(){ 

        var dataArray = $("#myForm").serializeArray(),
        dataObj = {};
        console.dir(dataArray); //require firebug
        //console.log(dataArray);

        $(dataArray).each(function(i, field){
          dataObj[field.name] = field.value;
        });

        var recaptcha = (dataObj['g-recaptcha-response']);

        if(recaptcha != "") {
                $( "#temp" ).remove();
        }       
    });

    $( ".register" ).click(function() {

        var dataArray = $("#myForm").serializeArray(),
            dataObj = {};
            console.dir(dataArray); //require firebug
            //console.log(dataArray);

        $(dataArray).each(function(i, field){
          dataObj[field.name] = field.value;
        });

        var recaptcha = (dataObj['g-recaptcha-response']);

        $( "#temp" ).remove();

            if(recaptcha == "") {
                $("#recaptcha").append('<label id="temp" style="color:red;line-height:normal;font-size: small;">This field is required.</label>');
            }

    });             

});

HTML:

<div class="g-recaptcha" name="recaptcha" id="recaptcha" data-sitekey="XXXXXXXXXXXXXXXXXXXXXXXXX"></div>   



回答3:


Your code:

<div class="g-recaptcha" name="recaptcha" id="recaptcha" data-sitekey="XXXXX"></div>

with...

recaptcha: {
    required: true
} 

You cannot validate a <div>. Period.

The jQuery Validate plugin can only validate <input>, <textarea> and <select> elements that are also within a <form></form> container.

The only workaround would be to copy your value into a type="hidden" input element and then apply your validation to that.




回答4:


hoping that you are using HTML5 form tag.

<form>
<div class="g-recaptcha" name="recaptcha" id="recaptcha" data-sitekey="XXXXX" required></div>
</form>

simply use required in the end



来源:https://stackoverflow.com/questions/28244729/set-recaptcha-field-as-required

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!