forms.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. (function() {
  2. 'use strict';
  3. window.addEventListener('load', function() {
  4. var form = document.getElementById('needs-validation');
  5. form.addEventListener('submit', function(event) {
  6. if (form.checkValidity() === false) {
  7. event.preventDefault();
  8. event.stopPropagation();
  9. }
  10. form.classList.add('was-validated');
  11. }, false);
  12. }, false);
  13. })();
  14. $("#contactForm").validator().on("submit", function (event) {
  15. if (event.isDefaultPrevented()) {
  16. formError();
  17. submitMSG(false, "Did you fill in the form properly?");
  18. } else {
  19. event.preventDefault();
  20. submitForm();
  21. }
  22. });
  23. function submitContactForm(){
  24. var name = $("#name").val();
  25. var email = $("#email").val();
  26. var email = $("#phone").val();
  27. var message = $("#message").val();
  28. $.ajax({
  29. type: "POST",
  30. url: "form/form.php",
  31. data: "name=" + name + "&email=" + email + "&phone=" + phone +"&message=" + message,
  32. success : function(text){
  33. if (text == "success"){
  34. formSuccess();
  35. } else {
  36. formError();
  37. submitMSG(false,text);
  38. }
  39. }
  40. });
  41. }
  42. function formSuccess(){
  43. $("#contactForm")[0].reset();
  44. submitMSG(true, "Message Submitted!")
  45. }
  46. function formError(){
  47. $("#contactForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
  48. $(this).removeClass();
  49. });
  50. }
  51. function submitMSG(valid, msg){
  52. if(valid){
  53. var msgClasses = "h3 text-center tada animated text-success";
  54. } else {
  55. var msgClasses = "h3 text-center text-danger";
  56. }
  57. $("#msgSubmit").removeClass().addClass(msgClasses).text(msg);
  58. }