Search code examples
javascriptjqueryrecaptcha

how to get the recaptcha value so that it can be used in a condition check


Before I added a recaptcha in my website, I have a form that used for user post all user inputs into my db. however I disabled the submit button if there's a field that's still empty, or valued 0. I used jQuery to done this

enter image description here

I've tried something like using val() method to catch the recaptcha valeu but seems not working.

This is my jQuery to enable submit or disable it after checking all the fields :

$('#identity-next').attr('disabled', true); //disable the button
function checkFilled() {
    $('form').on('keyup change', function () {
        if ($(name).val() != "" 
         && $(birth_place).val() != 0 
         && $(birth_date).val() != "" 
         && $(email).val() != "" 
         && $(phone_number).val() != "" 
         && $(address_province).val() != 0 
         && $(address_regency).val() != 0 
         && $(address_district).val() != 0 
         && $(specify_address).val() != 0 
         && $(captcha).val() != "") // the one that's not working { 
            $('#identity-next').attr('disabled', false); return true;
        } 
        else { 
            $('#identity-next').attr('disabled', true); 
            return false; 
        }
    }); 
}

checkFilled() //call the function

this is my captcha div :

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

{!! NoCaptcha::renderJs() !!}

<span class="text-danger">{{ $errors->first('g-recaptcha-response') }} 
</span>

so I want my jquery function could handle the same functionality to enable the submit button when all fields not empty including my recaptcha, I'm using recaptcha V2 btw. in this condition, my button will keep disabled coz the captcha return no value at all(?).

Best regards!.


Solution

  • The solution to your problem is this:

    if($("#g-recaptcha-response").val()!="") {
        //show button
    }
    

    enter image description here