Code to Block submission of form by pressing Enter Key



Code to Block submission of form by pressing Enter Key..

Answer / 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

More JavaScript Code Interview Questions

could you please tell me for what javac, javax,swing,awt.*; are using. what is the meaning and differences. and also there are 4 access modifiers in java. public, private, protected, default. what is the meaning of these 4 access modifiers and difference. public is the access modifier void means return data type disp() means function name what mean by static

1 Answers   Infosys,


how to create an anonymous function

1 Answers  


code to create a new window

1 Answers  


how to create Expandable and Collapsible Menus

1 Answers  


advance the focus to next consecutive fields when Enter Key is pressed

1 Answers   TCS,


i am making a purchase sheet in html ,it contain names of product(by selecting checkboxes),quantity(textbox) and price(checbox), by using java script i am restricting the user to prevent them to enter the wrong data ,so i made the servlet were i am only able to prevent them to enter the page in empty field , so my doubt is how do you prevent them how to enter non numerical data in quantity field

2 Answers   Infosys,


function to combine two or more arrays

1 Answers   Aricent,


how to create a Draggable element

1 Answers   TCS,


write a function to validate a given date

1 Answers  


maximizing the main window

1 Answers  


Code to Block submission of form by pressing Enter Key

1 Answers   TED,


I have a doubt regarding including tags in a function. I have written a function in javascript in a html page. The function got called by clicking a button, i want to display the results in same html page by placing tags in the function. (this hmtl page is static page) Is this possible? example: <SCRIPT LANGUAGE=javascript> function calSubmit_onclick() { var xyz=0; .......... .......... document.write("<lable>"+xyz+"</label>"); /*Is it possible*/ }

1 Answers  


Categories
  • PHP Code Interview Questions PHP Code (33)
  • JSP Code Interview Questions JSP Code (6)
  • ASP Code Interview Questions ASP Code (5)
  • CGI Perl Code Interview Questions CGI Perl Code (3)
  • JavaScript Code Interview Questions JavaScript Code (63)
  • VB Script Code Interview Questions VB Script Code (20)
  • Shell Script Code Interview Questions Shell Script Code (31)
  • Python Code Interview Questions Python Code (34)
  • WinRunner Code Interview Questions WinRunner Code (1)
  • HTML DHTML XHTML Code Interview Questions HTML DHTML XHTML Code (13)
  • XML Interview Questions XML (43)
  • Scripts_Markup Code AllOther Interview Questions Scripts_Markup Code AllOther (5)