Web app - need to stop enter key from triggering a button.

Posted on 2004-11-09
Last Modified: 2012-08-13

     I'm designing a VB.NET webform that has a small javascript that places the focus initially in a textbox.  At the bottom are buttons - Add/Edit/Cancel/Delete.  When I hit the enter key at any point on this form, the Add button is triggered (although a few times it's been edit) which disrupts the form.  I want the user to have to click the buttons, or later I will program so that depending on what text box the focus is in triggers different buttons.  I don't ever want enter key to trigger the Add button.  I need a javascript either in my HTML side or that I can just submit the way I do my focus:

            Dim strScript As String = "<script language=JavaScript> document.all('txtScanStock').focus(); </script>"
            Page.RegisterStartupScript("MyScript", strScript)

that will keep the enter key from submitting random buttons, or any at all.  Can someone help me?  Thanks!

Question by:gleznov
    LVL 19

    Accepted Solution

    Will something like this help?  The only part you need is the <script> in the head, but I put together a form as a sample of capturing the Enter key and ignoring it.  There's an alert in there also just to illustrate that it's working, but you can just comment it out once you're satisfied it works.  I put a comment right above it, so you'll know which line to comment out. Hope that helps.

    <script type="text/javascript">
    document.onkeypress = ignoreEnter;

    function ignoreEnter(e)
      var theKey = window.event? event.keyCode: e.which;
      if (theKey == 13)
        if (window.event)
          event.cancelBubble = true;
        // comment out the line after this to get rid of the alert
        alert("captured enter, but ignoring it");
        return false;

    <form onsubmit="alert('you submitted form'); return false;">
        <label for="text1">Text1:</label>
        <input type="text" id="text1" />
      <input type="submit" value="Submit the Form" />

    Author Comment

    That's absolutely perfect!!!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Javascript: Crash Course

    Javascript is not only emerging as the most important language to know, it's flexibility is unique. With Javascript being deployed on the web, mobile and even the desktop this is the one language that all technologists need to know.

    In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now