Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to prevent auto submit of form when i press enter key on textbox?
Posted

XML
<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.
 
Share this answer
 
v2
Comments
sankar guru 21-Sep-10 6:56am    
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;
}



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?
XML
<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);" />
 
Share this answer
 
<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>
 
Share this answer
 
v2
Comments
sankar guru 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900