Source Code:
(back to article)
Submit
Result:
Report an issue
<form id="loginForm"> Username: <input type="text" name="username" required> Password: <input type="password" name="password" required> <button type="submit">Login</button> </form> <script> document.getElementById('loginForm').addEventListener('submit', function(event) { event.preventDefault(); // Prevent the form from submitting normally if (this.username.value.length < 4 || this.password.value.length < 4) { alert('Username and password must be at least 4 characters long.'); } else { this.submit(); // Submit the form manually if validation passes alert('successfully submitted'); } }); </script>