Avoid a beep when typing a key in a text field

When you create an aspx page as follows:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </div> </form> </body> </html> 

how can you avoid the sound signal that occurs when you are in the text box and press enter.

On the other hand, I would like to handle the enterkeypress event.

Tx!

+7
javascript onkeypress
source share
8 answers

If it’s related to the browser that your audience is using, then they’re used to it because it happens on every site other than you. I would say that this is not worth worrying - sort of like people with JavaScript turned off. They know that they are disabled, and as a result, they are used to sites that do not have specific functionality. You cannot force them to turn it on, and you cannot force them to turn off the sound. This is one of the factors that you can accept in web applications.

0
source share

First of all, this is not standard behavior to get a sound when you press enter in the text box on a web page. Try finding the Google search page or, for that matter, try the Name, Email, and Home fields at the bottom of this page. None of them beeps when you press enter - in any browser.

To prevent a beep, handle the onKeyDown event in the <INPUT> and return false if the enter key is pressed:

 <input type="text" name="TextBox1" value="" onKeyDown="return StopBeepOnEnter(event)" /> <script type="text/javascript"> function StopBeepOnEnter(event) { if (event.keyCode == 13) { // Do Whatever... return false; // This stops the beep } } </script> 

And here is the same with jQuery (I really tested this and it solved the problem for me):

 <script type="text/javascript"> $("#<%=TextBox1.ClientID %>").keydown(function(event) { if (event.keyCode == 13) { // Do Whatever... return false; // This stops the beep } }); </script> 

I found a solution in this thread - Tarn Jaggar loan:

http://codingforums.com/showthread.php?t=36491

+10
source share

A few things to note here ...

  • A warning will occur if the submit button is hidden or there is no submit button at all. This often happens if you manually press your own <a> buttons, as I usually do (since the OS buttons are ugly by default). One thing you might want to learn is to use <button type="submit"></button> , because you can style them a lot more than the standard input buttons, and they will handle key events organically.

  • A warning will still occur if you intercept keyCode 13 on keyup or keydown . I do not know what this is about these events, but you should use keypress . This is what I usually do with jQuery globally ...

     $("input[type=text],input[type=password]").keypress(function(e) { if(e.keyCode == 13) { $(this).closest("form").trigger("submit"); e.preventDefault(); } }); 

    And then at the page level I delay the send event and execute my validation procedures ...

     $("form").submit(function(e) { var txt = $("#txtUser"); if(txt.val().length == 0) { alert("Who are you?"); txt.focus(); e.preventDefault(); } }); 
+4
source share

Put this in your form tag onkeypress="if(event.keyCode==13)return false;"

+1
source share

You must disable it in your sound settings in your operating system. This is an Internet Explorer application.

0
source share

I did nothing in asp, but at first I thought it would look like a Winforms application. My first step in solving this issue would be to set the key (code, key code, everything that he called in the event) var to # 0 or null, as well as any vars such as e.handled in the event call before returning.

0
source share

Finally, I can turn off the beep on all components of the page by pressing the Enter key in the text box or combo or any component that was needed. What I did, I added [onkeypress = "if (event.keyCode == 13) return false;" ] in the body tag, as shown below:

  <body onkeypress="if(event.keyCode==13)return false;"> 

I used the code above in my GWT GXT application.

Krishan Babbar

0
source share

KeyPressEvent.preventDefault() seems to do the trick in GWT

0
source share

All Articles