(function ($) { "use strict"; $(document).ready(function () { var $form = $("#pq_demo_form"); if (!$form.length) return; var $nome = $("#demo_nome"); var $email = $("#demo_email"); var $privacy = $("#demo_privacy"); var $honeypot = $("#demo_website"); var $telefono = $("#demo_telefono"); var $azienda = $("#demo_azienda"); var $messaggio = $("#demo_messaggio"); var csfrtok = $('#csfrtok').val(); function setError(key, message) { var $group = $form.find('[data-field="' + key + '"]'); var $error = $("#demo_" + key + "_error"); var $input = null; if (key === "nome") $input = $nome; if (key === "email") $input = $email; if (key === "privacy") $input = $privacy; if ($group.length) $group.addClass("is-invalid"); if ($error.length) $error.text(message); if ($input && $input.length) $input.attr("aria-invalid", "true"); } function clearError(key) { var $group = $form.find('[data-field="' + key + '"]'); var $error = $("#demo_" + key + "_error"); var $input = null; if (key === "nome") $input = $nome; if (key === "email") $input = $email; if (key === "privacy") $input = $privacy; if ($group.length) $group.removeClass("is-invalid"); if ($error.length) $error.text(""); if ($input && $input.length) $input.removeAttr("aria-invalid"); } function isValidEmail(value) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value); } // Validazione live if ($nome.length) { $nome.on("input", function () { if ($nome.val().trim() !== "") clearError("nome"); }); } if ($email.length) { $email.on("input", function () { var value = $email.val().trim(); if (value !== "" && isValidEmail(value)) clearError("email"); }); } if ($privacy.length) { $privacy.on("change", function () { if ($privacy.is(":checked")) clearError("privacy"); }); } // Invio del form $form.on("submit", function (event) { event.preventDefault(); var invalid = false; clearError("nome"); clearError("email"); clearError("privacy"); // Controllo Honeypot anti-spam if ($honeypot.length && $honeypot.val().trim() !== "") { return false; } if (!$nome.length || $nome.val().trim() === "") { setError("nome", "Inserisci il tuo nome e cognome."); invalid = true; } if (!$email.length || $email.val().trim() === "") { setError("email", "Inserisci la tua email."); invalid = true; } else if (!isValidEmail($email.val().trim())) { setError("email", "Inserisci un indirizzo email valido."); invalid = true; } if (!$privacy.length || !$privacy.is(":checked")) { setError("privacy", "Devi accettare l’informativa privacy per proseguire."); invalid = true; } if (invalid) { var $firstInvalid = $form.find("[aria-invalid='true']").first(); if ($firstInvalid.length) $firstInvalid.focus(); return false; } // ========================================== // CHIAMATA AJAX CON JQUERY // ========================================== var formData = { nome: $nome.length ? $nome.val().trim() : "", email: $email.length ? $email.val().trim() : "", telefono: $telefono.length ? $telefono.val().trim() : "", azienda: $azienda.length ? $azienda.val().trim() : "", messaggio: $messaggio.length ? $messaggio.val().trim() : "", csfrtok: csfrtok, website: $honeypot.length ? $honeypot.val().trim() : "", }; $.ajax({ url:$('#base_url').val() + '/ajax/send_message_richiesta_contatto', type: "POST", dataType: "json", data: formData, success: function (response) { alert("Messaggio inviato."); $form[0].reset(); // Eventuale reset del form }, error: function (xhr, status, error) { console.error("Errore AJAX:", error); alert("Si è verificato un errore durante l'invio."); } }); }); }); })(jQuery);