Home All Groups Group Topic Archive Search About
Author
17 Sep 2007 6:22 AM
peter
On my page, there are several textboxes, a Save button and a Cancel Button.
When u press enter key in any of the textboxes, it does the same thing as u
click on the Save button. Which is not I wanted. I wish pressing Enter do
nothing.

Anyone can help me to solve this problem?

Peter

Author
17 Sep 2007 2:46 PM
David R. Longnecker
Peter-

Usability aside (people expect that Enter does something, so disabling it
can confuse some), JavaScript can catch and then disregard the Enter key.

<head runat="server">
    <script type="text/javascript">

    function catchEnterKey()
    {
        if (window.event.keyCode == 13)
        {
            event.returnValue=false;
            event.cancel = true;
        }
    }
    </script>
</head>

Then, on your body tag:

<body onkeydown="catchEnterKey()">

Another way to do this would be to have a specified button you want to execute
and use the form's defaultbutton attribute to set it to that form.  If you
have required fields, use validators to ensure they are filled in, etc. prior
to submission.

HTH.

-dl


--
David R. Longnecker
http://blog.tiredstudent.com

Show quote
> On my page, there are several textboxes, a Save button and a Cancel
> Button. When u press enter key in any of the textboxes, it does the
> same thing as u click on the Save button. Which is not I wanted. I
> wish pressing Enter do nothing.
>
> Anyone can help me to solve this problem?
>
> Peter
>

AddThis Social Bookmark Button