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


     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!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gleznovAuthor Commented:
That's absolutely perfect!!!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.