poineering the future of villages

shareholders

document.addEventListener("DOMContentLoaded", function () { const forms = document.querySelectorAll("form"); forms.forEach(form => { form.addEventListener("submit", function (event) { let isValid = true; const requiredFields = form.querySelectorAll("[required]"); requiredFields.forEach(field => { if (!field.value.trim()) { isValid = false; field.classList.add("error"); addErrorMessage(field, "This field is required."); } else if (field.name === "contactNumber") { // Adjust to your contact field name const contactPattern = /^\+?\d{7,15}$/; // Accepts 7-15 digits, with optional + for country code const cleanedValue = field.value.replace(/\D/g, ""); // Remove non-numeric characters if (!contactPattern.test(cleanedValue)) { isValid = false; field.classList.add("error"); addErrorMessage(field, "Enter a valid contact number (7-15 digits, optional country code)."); } else { clearErrorMessage(field); } } else { clearErrorMessage(field); } }); if (!isValid) { event.preventDefault(); alert("Please complete all required fields correctly."); } }); }); function addErrorMessage(field, message) { clearErrorMessage(field); const errorMessage = document.createElement("span"); errorMessage.textContent = message; errorMessage.classList.add("error-message"); field.insertAdjacentElement("afterend", errorMessage); } function clearErrorMessage(field) { field.classList.remove("error"); if (field.nextElementSibling?.classList.contains("error-message")) { field.nextElementSibling.remove(); } } });