Recaptcha validation does not work [closed]

1

I need to validate Recaptcha in a form. But, it's not working.

function validaCaptchaContato() {
  if(document.querySelector('#g-recaptcha-contato').value == '') {
     alert('Marque a opção "Não sou um robô"');
     return false;
  }
}
<form method="post" id="form-contato" onsubmit="return validaCaptchaContato();">
	BLABLABLABLA
	<label>
		<textarea class="fields textarea" name="mensagem" placeholder="Escreva sua mensagem." required></textarea>
	</label>
	<div class="captchaFix">
		<div class="g-recaptcha" data-sitekey="COPIEI CERTO"></div>
	</div>
	<input class="btn-submit" type="submit" name="enviar" id="g-recaptcha-contato" value="Enviar">
</form>

What could be happening?

    
asked by anonymous 10.01.2018 / 13:56

2 answers

0

You can use reCaptcha as follows in JavaScript:

You need to include the callback of the API, initially set to false :

googlerecpchk = false;
function recaptchaCallback() {
   googlerecpchk = true; // validou o captcha
};

After this, verify that it has been validated:

if(!googlerecpchk){
   alert('Marque que não é um robô');
}

In your case, you can use the structure:

googlerecpchk = false;
function recaptchaCallback() {
   googlerecpchk = true;
};

function validation() {
    if(!googlerecpchk){
       alert('Marque a opção "Não sou um robô"');
       return false;
    }
    alert('captcha OK!');
}
    
11.01.2018 / 03:34
0

I block the send button and use the data-callback attribute, so when the user checks, the button will be released.

function enableButton() {
  document.querySelector("input[type=\"submit\"]").removeAttribute("disabled");
}
<script src="https://www.google.com/recaptcha/api.js"></script><formaction="?" method="POST">
  <div class="g-recaptcha" data-sitekey="6LdOQBsTAAAAAHgHKPaN0mjDcEAWbC0f-YgSC65D" data-callback="enableButton"></div>
  <br/>
  <input type="submit" value="Submit" disabled>
</form>

You can also use the getResponse method by clicking the button, like this:

function validation() {
  if (grecaptcha.getResponse() == "") {
    alert("Robôs não podem enviar esse formulário");
    return false;
  }
  return true;
}
<script src="https://www.google.com/recaptcha/api.js"></script><formaction="?" method="POST" onsubmit="return validation();">
  <div class="g-recaptcha" data-sitekey="6LdOQBsTAAAAAHgHKPaN0mjDcEAWbC0f-YgSC65D" data-callback="enableButton"></div>
  <br/>
  <input type="submit" value="Submit">
</form>
    
10.01.2018 / 14:11