github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/docs/assets/js/contact-form.js (about) 1 2 /* Contact form */ 3 $(function() { 4 var submitButton = $("#submit-button"); 5 var form = $("#contact-form"); 6 7 selectPlanFromUrl(); 8 9 submitButton.on("click", submitForm); 10 11 function selectPlanFromUrl() { 12 var params = window.location.search; 13 if (params === "?plan=enterprise") { 14 var plan = $("#enterprise"); 15 plan.prop('checked', true); 16 } else { 17 var plan = $("#pro"); 18 plan.prop('checked', true); 19 } 20 } 21 22 function submitForm(e) { 23 e.preventDefault(); 24 25 if(validateForm()) { 26 var data = form.serialize(form.get(0), { hash: true }); 27 submitToFormSpree(data); 28 } 29 30 function submitToFormSpree(data) { 31 submitButton.html("Sending..."); 32 submitButton.prop("disabled", true); 33 34 var postParams = { 35 url: form.attr('action'), 36 type: "POST", 37 data: data, 38 dataType: "json" 39 }; 40 41 $.ajax(postParams) 42 .done(function() { 43 inCall = false; 44 window.location.replace("/thanks"); 45 }) 46 .fail(function() { 47 showFormError( 48 "Oops, something went wrong! Please try again. If the issue persists please email us directly at info@gruntwork.io" 49 ); 50 inCall = false; 51 submitButton.html("Submit"); 52 submitButton.prop("disabled", false); 53 }); 54 } 55 56 function showInputError(el) { 57 $(el).addClass("has-error"); 58 }; 59 60 function showFormError(message) { 61 $("#error-message").html( 62 '<h3 class="text-danger text-center">' + message + "</h3>" 63 ); 64 }; 65 66 function clearErrors() { 67 $("#error-message").html(""); 68 form.find("*").removeClass("has-error"); 69 }; 70 71 function validateForm() { 72 var isValid = true; 73 74 clearErrors(); 75 76 form.find("[required]").each(function(index, el) { 77 if (!$(el).val()) { 78 isValid = false; 79 showInputError(el); 80 showFormError("Please fill in all required fields"); 81 } 82 }); 83 84 return isValid; 85 }; 86 } 87 });