$("#contactFormAffitto").validator().on("submit", function (event) { if (event.isDefaultPrevented()) { // handle the invalid form... formError(); submitMSGAffitto(false, "Non hai compilato il modulo correttamente"); } else { // everything looks good! event.preventDefault(); submitFormAffitto(); } }); function submitFormAffitto(){ // Initiate Variables With Form Content var json = $("#contactFormAffitto").serialize(); $.ajax({ type: "POST", url: "https://www.turismoinvestimenti.com/send-prodotto-hsja.php", data: json, success : function(text){ //alert(text); if (text == "success"){ formSuccess(); } else { formError(); submitMSGAffitto(false,text); } } }); } function formSuccess(){ $("#contactFormAffitto")[0].reset(); submitMSGAffitto(true, "Messaggio Inviato con Successo!") } function formError(){ $("#contactFormAffitto").removeClass().addClass("shake animated").one("webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend", function(){ $(this).removeClass(); }); } function submitMSGAffitto(valid, msg){ if(valid){ var msgClasses = "h3 text-center tada animated text-success"; } else { var msgClasses = "h3 text-center text-danger"; } $("#msgSubmitAffitto").removeClass().addClass(msgClasses).text(msg); }