Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
how to prevent auto submit of form when i press enter key on textbox?
Posted 21-Sep-10 0:11am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

<script type="text/jscript" language="javascript">
    function submitButton() {
        if (window.event.keyCode == 0) {
            return false;
        }
        else {
            return true;
        }
    }
</script>
 
your button should like this:
<form id="form1" runat="server" onsubmit="return submitButton()">
 
If this is correct then Marked as Answer.
  Permalink  
v2
Comments
usankardurai at 21-Sep-10 6:56am
   
<script language ="javascript" type ="text/javascript" > function FocusNext(cid) { var i=document.getElementById (cid.id).getAttribute ('tabindex'); var iKeyCode = 0; if (window.event) iKeyCode = window.event.keyCode else if (e) iKeyCode = e.which; if (iKeyCode == 13) { var ctrl=document.all; for(var count = 0; ctrl.length ; count ++) { if(ctrl[count].tabIndex == i+1) { ctrl[count].focus(); } } } else return true; } </script> i have three textbox in my page ,when i press enter key on textbox the next textbox focussed ,so its working properly but i add button control on page means it wont work, how to solve it?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

<script type="text/javascript">
    function disableSubmitOnEnter(event) {
        if (!event)
            event = window.event
        var keycode;
        if (event.which)
            keycode = event.which
        else
            var keycode = event.keyCode
        if (keycode == 13)
            return false;
        else return true;
    }
</script>
 
<input type="text" onkeydown="return disableSubmitOnEnter(event);" />
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

<html>
<head>
<title>new document</title>
<script language="javascript" type="text/javascript">
<!--function goNext(the_val, field_name)
{    
if (the_val.length == 3) 
{        
document.forms['the_form'].elements[field_name].focus();    
}
}
-->
</script>
</head>
<body>
<form name="the_form">
<input type="text" name="tb1" onkeyup="goNext(this.value, 'tb2');"><br>
<input type="text" name="tb2" onkeyup="goNext(this.value, 'tb3');"><br>
<input type="text" name="tb3" onkeyup="goNext(this.value, 'tb4');"><br><input type="text" name="tb4" onkeyup="goNext(this.value, 'submit1');"><br>
<input type="submit" name="submit1" value="this is the submit button">
</form>
</body>
</html>
  Permalink  
v2
Comments
usankardurai at 21-Sep-10 7:48am
   
Thank U for U R Reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 456
1 OriginalGriff 183
2 Sergey Alexandrovich Kryukov 171
3 Tadit Dash 160
4 praveen_07 115
0 Sergey Alexandrovich Kryukov 9,280
1 OriginalGriff 5,473
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid