The issue I’m experiencing with my script is that it is not properly validating whether the Google reCAPTCHA is completed before allowing users to submit the form. While the validations for inputs are working as expected and appropriately prevent submission when those fields are invalid, the validation logic for the reCAPTCHA appears to be Ignored entirely, allowing users to bypass the Captcha.
<form id="of_form_empresa">
<div class="both">
<input type="text" name="name" id="of_nome" class="of_inputtext_s" placeholder="Nome" onfocus="clearinput(this)">
<input type="text" name="mail" id="of_mail" class="of_inputtext_s" placeholder="E-mail" onfocus="clearinput(this)">
</div>
<div class="both">
<input type="text" name="phone" id="of_fone" class="of_inputtext" placeholder="Telefone" onfocus="clearinput(this)">
</div>
<div class="both">
<textarea name="msg" id="of_msg" class="of_textarea" placeholder="Mensagem" onfocus="clearinput(this)"></textarea>
</div>
<div class="col-md-12 alcenter">
<div id="recaptcha-empresa" class="g-recaptcha" data-sitekey="MY-SITE-KEY"></div>
</div>
<div class="both">
<input type="button" value="Enviar" class="of_inputbutton" onclick="sendcontato()">
</div>
</form>
<script>
function sendcontato(){
form_ok = true;
var form = document.getElementById("of_form_empresa");
var name = form.elements['of_nome'].value;
var mail = form.elements['of_mail'].value;
var phone = form.elements['of_fone'].value;
var msg = form.elements['of_msg'].value;
var atpos=mail.indexOf("@");
var dotpos=mail.lastIndexOf(".");
if (name == ""){
$("#of_nome").addClass("of_invalid");
form_ok = false;
}else{
$("#of_nome").removeClass("of_invalid");
}
if (mail == "" || atpos<1 || dotpos<atpos+2 || dotpos+2>=mail.length){
$("#of_mail").addClass("of_invalid");
form_ok = false;
}else{
$("#of_mail").removeClass("of_invalid");
}
if (phone == ""){
$("#of_fone").addClass("of_invalid");
form_ok = false;
}else{
$("#of_fone").removeClass("of_invalid");
}
if (msg == ""){
$("#of_msg").addClass("of_invalid");
form_ok = false;
}else{
$("#of_msg").removeClass("of_invalid");
}
// Validate Captcha
const captchaResponse = grecaptcha.getResponse();
if (captchaResponse == "") {
alert("Please complete the captcha.");
form_ok = false;
}
if (form_ok){
$.post("includes/envia.php",{name:name,mail:mail,phone:phone,msg:msg},function(data){
// console.log(data);
$("#of_okconsulta").fancybox().trigger('click');
document.getElementById("of_form_empresa").reset();
});
}
}
</script>