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

Disable Keys in Forms

P: n/a
Hello,

Is it possible to disable a form submission when the Return key is
pressed?

I want the form to be submitted only when a button is pressed.

Thank you,

Miguel

Dec 18 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi,
You can write following javascript function:

<script language=javascript>
document.onkeydown = function(){
if(window.event && window.event.keyCode == 13)
{
return false;
// or else you can also write window.event.keyCode =0
}
}

Thanks and Regards,
Manish Bafna.
MCP and MCTS

</script>
"shapper" wrote:
Hello,

Is it possible to disable a form submission when the Return key is
pressed?

I want the form to be submitted only when a button is pressed.

Thank you,

Miguel

Dec 18 '06 #2

P: n/a
"Manish Bafna" <Ma*********@discussions.microsoft.comwrote in message
news:63**********************************@microsof t.com...
You can write following javascript function:

<script language=javascript>
That syntax is not XHTML-compliant...
document.onkeydown = function(){
if(window.event && window.event.keyCode == 13)
{
return false;
// or else you can also write window.event.keyCode =0
}
}
That will only work in IE...

<script type="text/javascript>

document.onkeyup = function keyPress(evt)
{
if(document.all)
{
var whichKey = window.event.keyCode;
}
else
{
var whichKey = evt.which;
}
if(whichKey == 13)
{
return false;
}
}

</script>
Dec 18 '06 #3

P: n/a
Hi Mark,

I tried your code but it does not work.

I found this one:

function checkCR(evt) {

var evt = (evt) ? evt : ((event) ? event : null);

var node = (evt.target) ? evt.target : ((evt.srcElement) ?
evt.srcElement : null);

if ((evt.keyCode == 13) && (node.type=="text")) {return false;}

}

document.onkeypress = checkCR;

It works but when the cursor is inside a password text box it does not
work.
So I removed (node.type=="text") and now it works both in Firefox and
IE and also for password type textboxes.

What do you think?

Thanks,
Miguel

Mark Rae wrote:
"Manish Bafna" <Ma*********@discussions.microsoft.comwrote in message
news:63**********************************@microsof t.com...
You can write following javascript function:

<script language=javascript>

That syntax is not XHTML-compliant...
document.onkeydown = function(){
if(window.event && window.event.keyCode == 13)
{
return false;
// or else you can also write window.event.keyCode =0
}
}

That will only work in IE...

<script type="text/javascript>

document.onkeyup = function keyPress(evt)
{
if(document.all)
{
var whichKey = window.event.keyCode;
}
else
{
var whichKey = evt.which;
}
if(whichKey == 13)
{
return false;
}
}

</script>
Dec 19 '06 #4

P: n/a
"shapper" <md*****@gmail.comwrote in message
news:11**********************@t46g2000cwa.googlegr oups.com...
I tried your code but it does not work.
Yes it does - what error(s) are you getting...?
Dec 19 '06 #5

P: n/a
I don't get an error. The form just submits itself when I press Return.

Any idea?

Thanks,
Miguel

P.S: The code I posted works only in Firefox. It seems it does not work
in IE either.

Mark Rae wrote:
"shapper" <md*****@gmail.comwrote in message
news:11**********************@t46g2000cwa.googlegr oups.com...
I tried your code but it does not work.

Yes it does - what error(s) are you getting...?
Dec 19 '06 #6

P: n/a
"shapper" <md*****@gmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...
>I don't get an error. The form just submits itself when I press Return.

Any idea?
Yes - see below...
P.S: The code I posted works only in Firefox. It seems it does not work
in IE either.
Then you've almost certainly set it up incorrectly...

Is the page in question available on the public internet...?
Dec 19 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.