I have a form where users scan in a barcode, the barcode reader automatically enters a carriage return in the value causing the form to submit since the browser chooses the first button as the default. How can I disable anything from happening when the enter key is pressed when entring a value in that textbox?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You’ll need to do it with javascript. In your markup for the text box, add an onkeydown handler like so:
<asp:TextBox ID="TextBox1" runat="server" onkeydown = "return (event.keyCode!=13);" > </asp:TextBox>
This will return false if the key was the enter key, which will cancel the form submission.
Method 2
Set the TextBox to have the text mode property set to “multiline”. Then the carriage return will be in the TextBox.
See also here for a note about what to do if you’re using FireFox.
Method 3
private void textbox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// do nothing
}
}
this may help you
Method 4
handle the onkeypress event and do something like this
if (e.KeyChar == (char)Keys.Enter)
{
// set event handled
e.Handled = true;
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0