var isSubmit = false; $(document).ready(function(){ $("#ib-probance-form").validate({ errorClass: 'error alert alert-danger', rules: { email: { required: true, email: true }, nombre: { required: true }, apellidos: { required: true }, terminos: { required: true } }, messages: { email: { required: "Debes introducir una dirección de correo electrónico", email: "Debe ser una dirección de correo electrónico válida" }, nombre: { required: "Debes introducir tu nombre" }, apellidos: { required: "Debes introducir tus apellidos" }, terminos: { required: "Debes aceptar la política de privacidad", } }, errorPlacement: function(error, element) { if(element.attr("name") == "terminos") error.appendTo(element.parent()); else error.insertAfter(element); }, submitHandler: function() { isSubmit = true; recaptcha(); } }); }); /* $('#ib-probance-form').on('submit', function(event) { if(isSubmit){ event.preventDefault(); grecaptcha.ready(function() { grecaptcha .execute('6Lf6bOIhAAAAABDDuCXtoX4SsYHRTrTH-OmOEEk2', {action: 'ib_probancenewsletter'}) .then(function(token) { let tokenInput = document.createElement('input'); tokenInput.name = "token"; tokenInput.value = token; tokenInput.type = "hidden"; let actionInput = document.createElement('input'); actionInput.name = "action"; actionInput.value = "ib_probancenewsletter"; actionInput.type = "hidden"; document.getElementById('ib-probance-form').appendChild(tokenInput); document.getElementById('ib-probance-form').appendChild(actionInput); $('#ib-probance-form').unbind('submit'); document.getElementById('ib-probance-form').submit.click(); }); }); } });*/ function recaptcha(){ grecaptcha.ready(function() { grecaptcha .execute('6Lf6bOIhAAAAABDDuCXtoX4SsYHRTrTH-OmOEEk2', {action: 'ib_probancenewsletter'}) .then(function(token) { let tokenInput = document.createElement('input'); tokenInput.name = "token"; tokenInput.value = token; tokenInput.type = "hidden"; let actionInput = document.createElement('input'); actionInput.name = "action"; actionInput.value = "ib_probancenewsletter"; actionInput.type = "hidden"; document.getElementById('ib-probance-form').appendChild(tokenInput); document.getElementById('ib-probance-form').appendChild(actionInput); $('#ib-probance-form').unbind('submit'); document.getElementById('ib-probance-form').submit.click(); }); }); }