Source Code:
(back to article)
Submit
Result:
Report an issue
<form id="contactForm" novalidate> <label for="email">Email:</label> <input type="email" id="email" pattern=".+@.+\..+" required /> <button type="submit">Submit</button> <span id="emailError" style="color: red"></span> <span id="successMessage" style="color: green; display: none;">Submission successful!</span> </form> <script> document.getElementById("contactForm").addEventListener("submit", function (event) { event.preventDefault(); // Prevent default form submission var email = document.getElementById("email"); var errorMessage = document.getElementById("emailError"); var successMessage = document.getElementById("successMessage"); if (!email.checkValidity()) { errorMessage.textContent = "Please enter a valid email address, including a domain."; // Display custom error message successMessage.style.display = "none"; // Hide success message if visible } else { errorMessage.textContent = ""; // Clear the error message successMessage.textContent = "Submission successful!"; successMessage.style.display = "block"; // Show success message email.value = ""; // Reset the email input } }); </script>