Source Code:
(back to article)
Submit
Result:
Report an issue
<form id="myForm"> Enter your name: <input type="text" name="username" required> <input type="submit" value="Submit"> </form> <div id="formFeedback"></div> <script> document.getElementById('myForm').addEventListener('submit', function(event) { var input = this.username.value; if (input.length < 4) { event.preventDefault(); // Prevent form from submitting document.getElementById('formFeedback').textContent = 'Name must be at least 4 characters long.'; } else { document.getElementById('formFeedback').textContent = 'Form submitted successfully!'; } }); </script>