Prevent postback on Enter in a asp.net inputfield

↘锁芯ラ 提交于 2019-12-05 21:27:57

Had a similar need to prevent an enter keypress (although, I didn't need to consider the input length condition as in this case) and found this which solved the problem... simpler than adding an entire script block if all you need to do is stop the form postback due to someone pressing enter in a text area.

<asp:TextBox ID="tb_Input" runat="server" onkeypress="return event.keyCode != 13;"></asp:TextBox>

Thought I'd share this in case someone else is searching for this functionality only.

<asp:TextBox ID="TextBox1" runat="server" onkeydown="return EnterEvent(event,this);" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged" />

function EnterEvent(e, ctrl) {
            var keycode = (e.keyCode ? e.keyCode : e.which);
            if (keycode == 13 && ctrl.value.length > 2) {
                return false;
            }
            else {
                return true;
            }
        }

Try:

e.preventDefault();

to stop the Enter event.

I've built a little fiddler:

http://jsfiddle.net/rblaettler/Y3b8F/5/

Is this what you are trying to do? It only submits if you have more than 2 characters.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!