By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

How to prevent the enter key causing a form postback .net 1.1

P: n/a
Hi all

I've got a very complex form with loads of text boxes and buttons on it.
It's causing me all sorts of problems when users press the enter key and
cause a postback.

I need to disable the enter key. I've done some surfing and found some
Microsoft recommended code but it doesn't seem to work and I'm wondering what
I've done wrong. If possible I'd like to disable the enter key for the whole
form not for each text box (that would be sooooo complicated to implement).

I'm using .Net 1.1 and vb.

Here's what I've got at the moment.

<HEAD>
<title>Raise CSD</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<SCRIPT language="javascript">
function DisableKeyPress()
{
//alert(window.event.keyCode)
if (event.keyCode == 13)
{
event.cancelbubble = true;
event.returnvalue = false;
}
}
</SCRIPT>
</HEAD>
<body onKeyPress="DisableKeyPress()" vLink="#000000" aLink="#d9d5d2"
link="#d7451a" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" >

then the code for the rest of the form.....

Thanks in advance!
Julia
Sep 17 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In the past, I did this with JavaScript, similar to what you have here.
Only, as I remember it, it wired up the Cancel event on body load and not
for each key press. I would google JavaScript enter key or even
event.keyCode == 13 for more suggestions.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:6F**********************************@microsof t.com...
Hi all

I've got a very complex form with loads of text boxes and buttons on it.
It's causing me all sorts of problems when users press the enter key and
cause a postback.

I need to disable the enter key. I've done some surfing and found some
Microsoft recommended code but it doesn't seem to work and I'm wondering
what
I've done wrong. If possible I'd like to disable the enter key for the
whole
form not for each text box (that would be sooooo complicated to
implement).

I'm using .Net 1.1 and vb.

Here's what I've got at the moment.

<HEAD>
<title>Raise CSD</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<SCRIPT language="javascript">
function DisableKeyPress()
{
//alert(window.event.keyCode)
if (event.keyCode == 13)
{
event.cancelbubble = true;
event.returnvalue = false;
}
}
</SCRIPT>
</HEAD>
<body onKeyPress="DisableKeyPress()" vLink="#000000" aLink="#d9d5d2"
link="#d7451a" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" >

then the code for the rest of the form.....

Thanks in advance!
Julia
Sep 17 '08 #2

P: n/a

Have found this and it works :-)

<Script language=javascript>
//this disables the enter key to stop form submission when the user
presses enter
document.onkeydown = doKey;

function doKey(E) {
if (event.keyCode==13)return false;
}
</script>

Thanks!
"Cowboy (Gregory A. Beamer)" wrote:
In the past, I did this with JavaScript, similar to what you have here.
Only, as I remember it, it wired up the Cancel event on body load and not
for each key press. I would google JavaScript enter key or even
event.keyCode == 13 for more suggestions.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:6F**********************************@microsof t.com...
Hi all

I've got a very complex form with loads of text boxes and buttons on it.
It's causing me all sorts of problems when users press the enter key and
cause a postback.

I need to disable the enter key. I've done some surfing and found some
Microsoft recommended code but it doesn't seem to work and I'm wondering
what
I've done wrong. If possible I'd like to disable the enter key for the
whole
form not for each text box (that would be sooooo complicated to
implement).

I'm using .Net 1.1 and vb.

Here's what I've got at the moment.

<HEAD>
<title>Raise CSD</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<SCRIPT language="javascript">
function DisableKeyPress()
{
//alert(window.event.keyCode)
if (event.keyCode == 13)
{
event.cancelbubble = true;
event.returnvalue = false;
}
}
</SCRIPT>
</HEAD>
<body onKeyPress="DisableKeyPress()" vLink="#000000" aLink="#d9d5d2"
link="#d7451a" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" >

then the code for the rest of the form.....

Thanks in advance!
Julia

Sep 17 '08 #3

P: n/a
"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:55**********************************@microsof t.com...

[top-posting corrected]
>>I need to disable the Enter key.

In the past, I did this with JavaScript, similar to what you have here.
Only, as I remember it, it wired up the Cancel event on body load and not
for each key press. I would google JavaScript enter key or even
event.keyCode == 13 for more suggestions.

Have found this and it works :-)

<Script language=javascript>
The above script declaration has been deprecated for ages - use this
instead:

<script type="text/javascript">
if (event.keyCode==13)return false;
This is not cross-browser compatible - it will work only in IE:
http://www.google.co.uk/search?sourc...eyCode+firefox
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Sep 17 '08 #4

P: n/a
Just be sure to test on other browsers, as some JavaScript only works with
some. Worst case is creating branches for different browsers.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:55**********************************@microsof t.com...
>
Have found this and it works :-)

<Script language=javascript>
//this disables the enter key to stop form submission when the user
presses enter
document.onkeydown = doKey;

function doKey(E) {
if (event.keyCode==13)return false;
}
</script>

Thanks!
"Cowboy (Gregory A. Beamer)" wrote:
>In the past, I did this with JavaScript, similar to what you have here.
Only, as I remember it, it wired up the Cancel event on body load and not
for each key press. I would google JavaScript enter key or even
event.keyCode == 13 for more suggestions.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:6F**********************************@microso ft.com...
Hi all

I've got a very complex form with loads of text boxes and buttons on
it.
It's causing me all sorts of problems when users press the enter key
and
cause a postback.

I need to disable the enter key. I've done some surfing and found some
Microsoft recommended code but it doesn't seem to work and I'm
wondering
what
I've done wrong. If possible I'd like to disable the enter key for the
whole
form not for each text box (that would be sooooo complicated to
implement).

I'm using .Net 1.1 and vb.

Here's what I've got at the moment.

<HEAD>
<title>Raise CSD</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<SCRIPT language="javascript">
function DisableKeyPress()
{
//alert(window.event.keyCode)
if (event.keyCode == 13)
{
event.cancelbubble = true;
event.returnvalue = false;
}
}
</SCRIPT>
</HEAD>
<body onKeyPress="DisableKeyPress()" vLink="#000000" aLink="#d9d5d2"
link="#d7451a" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" >

then the code for the rest of the form.....

Thanks in advance!
Julia

Sep 17 '08 #5

P: n/a
This should be OK for me as it's for an intranet application and all users
will using IE.

Thanks for the warning anyway.

Julia

"Cowboy (Gregory A. Beamer)" wrote:
Just be sure to test on other browsers, as some JavaScript only works with
some. Worst case is creating branches for different browsers.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:55**********************************@microsof t.com...

Have found this and it works :-)

<Script language=javascript>
//this disables the enter key to stop form submission when the user
presses enter
document.onkeydown = doKey;

function doKey(E) {
if (event.keyCode==13)return false;
}
</script>

Thanks!
"Cowboy (Gregory A. Beamer)" wrote:
In the past, I did this with JavaScript, similar to what you have here.
Only, as I remember it, it wired up the Cancel event on body load and not
for each key press. I would google JavaScript enter key or even
event.keyCode == 13 for more suggestions.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"Julia B" <Ju****@discussions.microsoft.comwrote in message
news:6F**********************************@microsof t.com...
Hi all

I've got a very complex form with loads of text boxes and buttons on
it.
It's causing me all sorts of problems when users press the enter key
and
cause a postback.

I need to disable the enter key. I've done some surfing and found some
Microsoft recommended code but it doesn't seem to work and I'm
wondering
what
I've done wrong. If possible I'd like to disable the enter key for the
whole
form not for each text box (that would be sooooo complicated to
implement).

I'm using .Net 1.1 and vb.

Here's what I've got at the moment.

<HEAD>
<title>Raise CSD</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<SCRIPT language="javascript">
function DisableKeyPress()
{
//alert(window.event.keyCode)
if (event.keyCode == 13)
{
event.cancelbubble = true;
event.returnvalue = false;
}
}
</SCRIPT>
</HEAD>
<body onKeyPress="DisableKeyPress()" vLink="#000000" aLink="#d9d5d2"
link="#d7451a" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" >

then the code for the rest of the form.....

Thanks in advance!
Julia


Sep 17 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.