<script>
$(document).ready(function() {
$('#contact-form .submit-btn').on('click', function(ev) {
ev.preventDefault();
let errors = [];
let valid = true;
const nameValid = $('#contact_name').val() != null && $('#contact_name').val().length > 0;
if (nameValid == false) {
valid = false;
errors.push('Nom* obligatoire.');
}
const emailLengthValid = $('#contact_email').val() != null && $('#contact_email').val().length > 0;
if (emailLengthValid == false) {
valid = false;
errors.push('Email* obligatoire.');
}
const messageValid = $('#contact_message').val() != null && $('#contact_message').val().length > 0;
if (messageValid == false) {
valid = false;
errors.push('Message* obligatoire.');
}
const isEmail = isEmailFormat($('#contact_email').val());
if (isEmail == false) {
valid = false;
errors.push('Email: format invalide.');
}
const validPrivacy = $('#clone_contact_privacyPolicy').prop('checked');
if (validPrivacy == false) {
valid = false;
errors.push('Accépter la politique de confidentialité.');
}
const validRecaptcha = validateRecaptcha();
if (validRecaptcha == false) {
valid = false;
errors.push('reCaptcha invalide.');
}
if (valid == true) {
$('#contact-form').submit();
} else {
let htmlError = '<div class="alert alert-danger">Veuillez vérifier ces informations s\'il vous plaît:<ul>';
errors.forEach((message, key) => {
htmlError += '<li>' + message + '</li>';
});
htmlError += '</ul></div>';
$('.alert-container').html(htmlError);
}
});
$('#clone_contact_privacyPolicy').on('click', function(ev) {
const checked = $(this).prop('checked');
$('#contact_privacyPolicy').prop('checked', checked);
});
});
/**
* @return {boolean} valid
*/
function validateRecaptcha() {
var response = grecaptcha.getResponse();
if (response.length === 0) {
console.warn("reCaptcha not validated");
return false;
} else {
console.log("reCaptcha validated");
return true;
}
}
/**
* @param {string} text
* @return {boolean} isEmail
*/
function isEmailFormat(text) {
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(text);
}
</script>