adspace


Code to Block submission of form by pressing Enter Key

Answer Posted / Puneet Kumar Srivastava

To prevent a form from being submitted when the Enter key is pressed, you can use JavaScript's event listeners:n```htmln<!DOCTYPE html>n<html lang="en">n<head>n <meta charset="UTF-8">n <title>Prevent Form Submission on Enter</title>n</head>n<body>n <!-- Your form here -->n <script>n document.getElementById('yourForm').addEventListener('submit', function(event) {n event.preventDefault(); // Prevents the default form submissionn });n document.addEventListener('keydown', function(event) {n if (event.key === 'Enter' && document.activeElement.tagName === 'INPUT') {n event.preventDefault(); // Prevents the form submission when Enter is pressed on an input fieldn }n });n </script>n</body>n</html>``` Replace `yourForm` with the id of your form.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

code to sorting an array of objects

2572