Source Code:
(back to article)
Submit
Result:
Report an issue
<form id="signupForm" novalidate> <label for="phone">Phone (XXX-XXX-XXXX):</label> <input type="tel" id="phone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" required> <button type="submit">Sign Up</button> <span id="phoneError" style="color:red;"></span> <span id="successMessage" style="color:green; display:none;">Submission successful!</span> </form> <script> document.getElementById('signupForm').addEventListener('submit', function(event) { var phone = document.getElementById('phone'); var phoneError = document.getElementById('phoneError'); var successMessage = document.getElementById('successMessage'); if (!phone.checkValidity()) { event.preventDefault(); phoneError.textContent = 'Please enter a phone number in the format XXX-XXX-XXXX.'; successMessage.style.display = 'none'; // Hide success message if present } else { phoneError.textContent = ''; phone.value = ''; // Reset the input field successMessage.textContent = 'Submission successful!'; successMessage.style.display = 'block'; // Show success message event.preventDefault(); // Prevent form submission for demonstration } }); </script>