← Back
Editing: formValidation.js
function validateForm(formId) { console.log('Form validation initialized for:', formId); // Attach event listener to the form submission jQuery(document).on("submit", `#${formId}`, function (event) { let isValid = true; let form = jQuery(this); // Get the submitted form // Find all required input and textarea fields within the form let fields = form.find("input[type='text'], input[type='email'], input[type='tel'], textarea"); fields.each(function () { let fieldElement = jQuery(this); let fieldValue = fieldElement.val().trim(); let fieldType = fieldElement.attr("type"); let errorContainer = fieldElement.next(".error-text, .error-email"); // Remove existing error messages before rechecking errorContainer.remove(); // Validate empty fields if (!fieldValue) { console.log(`Validation failed: ${fieldElement.attr("name") || fieldElement.attr("id")} is empty`); fieldElement.addClass("error-border"); fieldElement.after('<span class="error-text" style="color:red; margin-top:6px;">Dette felt må ikke være tomt</span>'); isValid = false; } // Validate email format (only if not empty) else if (fieldType === "email" && !isValidEmail(fieldValue)) { console.log(`Validation failed: ${fieldElement.attr("name") || fieldElement.attr("id")} has invalid email`); if (errorContainer.length === 0) { // Avoid duplicate error messages fieldElement.addClass("error-border"); fieldElement.after('<span class="error-email" style="color:red; margin-top:6px;">Ugyldig e-mail</span>'); } isValid = false; } else { fieldElement.removeClass("error-border"); } }); if (!isValid) { console.log("Form validation failed. Preventing submission."); event.preventDefault(); // Stop form submission if validation fails return false; // return false to prevent submission } console.log("Form validation passed. Submitting form."); }); // Remove error while typing jQuery(document).on("input", `#${formId} input, #${formId} textarea`, function () { let fieldElement = jQuery(this); let fieldType = fieldElement.attr("type"); let fieldValue = fieldElement.val().trim(); let errorContainer = fieldElement.next(".error-text, .error-email"); // Remove errors for all fields except email validation if (fieldType !== "email") { fieldElement.removeClass("error-border"); errorContainer.remove(); } // Special case: Email validation should persist if invalid if (fieldType === "email") { let errorEmailContainer = fieldElement.next(".error-email"); let emailValue = $(this).val().trim(); if (isValidEmail(emailValue)) { $(this).removeClass("error-border"); // Reset border color $(this).next("span.error-text").remove(); // Remove error message $(this).next("span.error-email").remove(); // Remove error message } else if (emailValue === '') { fieldElement.addClass("error-border"); fieldElement.after('<span class="error-text" style="color:red; margin-top:6px;">Dette felt må ikke være tomt</span>'); if (errorEmailContainer.length > 0) { $("span.error-email").remove(); } console.log(errorEmailContainer.length) } else { $(this).removeClass("error-border"); // Reset border color $(this).next("span.error-text").remove(); // Remove error message if (errorEmailContainer.length === 0) { // Avoid adding multiple error messages fieldElement.addClass("error-border"); fieldElement.after('<span class="error-email" style="color:red; margin-top:6px;">Ugyldig e-mail</span>'); } } } }); } // Function to check valid email format function isValidEmail(email) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } // Call the function to initialize validation jQuery(document).ready(function ($) { if ($("#gform_8").length > 0) { validateForm("gform_8"); } if ($("#gform_11").length > 0) { validateForm("gform_11"); } if ($("#gform_12").length > 0) { validateForm("gform_12"); } });
Save File
Cancel