Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How can I disable the Tab and Enter Key?

Posted on 2006-03-24
24
Medium Priority
?
1,816 Views
Last Modified: 2008-01-16
Hi Experts,

How can I disable the Tab and Enter Key?

Thanks
0
Comment
Question by:indyng
  • 8
  • 7
  • 5
  • +1
24 Comments
 
LVL 13

Expert Comment

by:kawas
ID: 16279800
add a key 'listener'

function myKeyListener(event) {
       if (!event) {
            event = window.event;
      }
      //alert (event.keyCode);

      if (event.keyCode == 13) {
            // enter key - do nothing
      }
       // add one for the tab key
}

then add this function to the onkeydown event for the page/textbox/etc
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21382788.html tries something similar
0
 
LVL 1

Author Comment

by:indyng
ID: 16279829
Is it possible to disable these keys onload of the "default.asp" page rather than onkeydown?

Thanks
0
 
LVL 13

Expert Comment

by:kawas
ID: 16279853
on the onload event, you would have to add the function so that onkeydown of whatever item you want the keys disabled
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:indyng
ID: 16280178
So this function is only fired when the user types?
0
 
LVL 13

Expert Comment

by:kawas
ID: 16280189
yes
0
 
LVL 1

Author Comment

by:indyng
ID: 16280435
I want to fire the function on load of the "default.asp" page.

Can you provide more specific code for this? I am sorry but I am not very good at Java script.

Thanks
0
 
LVL 13

Expert Comment

by:kawas
ID: 16280505

Steps are:

1. <body onload="init()">

2. define init()

function init() {
   document.onkeypress = myKeyListener;
   if (document.layers) document.captureEvents(Event.KEYPRESS);
}
function myKeyListener(event) {
       if (!event) {
          event = window.event;
     }
     //alert (event.keyCode);

     if (event.keyCode == 13) {
          // enter key - do nothing
     }
       // add one for the tab key
}


or a shorter example
http://www.felgall.com/jstip43.htm
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16280780
@kawas: A bit nonchalant to not kill the event, to leave the tab key as an exercise for the reader and to not handle other browsers than IE
0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 16280790
Following diables tab and enter key for all fields on page
<script language="javascript">
function keyHandler(e) {
var pK = e ? e.which : window.event.keyCode;
if (pk == 13) { return false; }
if (pk == 9) { return false; }
}

function DisableEnterAndTab () {
      if (document.all) // IE
     {
     document.onkeypress = kH;
     }
      if (document.layers)  {
        document.captureEvents(Event.KEYPRESS);
     }
}
</script>

<body onload="DisableEnterAndTab();">
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16280828
0
 
LVL 13

Expert Comment

by:kawas
ID: 16280843
that disables on a textfield, not the whole page
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16280867
@pravinsar :Please test your code. Especially when the asker tells you he is not great at JS - document.all is IE and document.layers is NS4 - what about mozilla and FF?

<script language="javascript">
function kH(e) {
var pK = e ? e.which : window.event.keyCode;
if (pk == 13) { alert('No enter'); return false; }
if (pk == 9) { alert('No tab'); return false; }
}

function DisableEnterAndTab () {
      if (document.all) // IE
     {
     document.onkeypress = kH;
     }
      if (document.layers)  {
        document.captureEvents(Event.KEYPRESS);
     }
}
</script>

<body onload="DisableEnterAndTab();">
<form onSubmit="alert('No you did not disable them'); return false">
<input type="text" name="t1" value="">
<input type="text" name="t2" value="">
<input type="submit">

</form>
0
 
LVL 1

Author Comment

by:indyng
ID: 16280952
This is what I have:

function init()
{
 document.onkeypress = myKeyListener;
 if (document.layers) document.captureEvents(Event.KEYPRESS);
}

function myKeyListener(event)
{
 if (!event)
 {
  event = window.event;
 }
 if (event.keyCode == 13)
 {

 }
}

I checked out http://www.felgall.com/jstip43.htm

I do not know how to incorporate this. Please be more specific and just provide me with the script.

Thanks
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16281042
@experts: I do not have time to find the combo I wanted to find.

Please look here and test before presenting
http://www.webmasterworld.com/forum91/5129.htm

Here is a cool site though. the prototype.js has a lot of cross browser event handling

http://prototype.conio.net/
0
 
LVL 1

Author Comment

by:indyng
ID: 16281193
This is what I have now and it works but only for Enter key:

<SCRIPT Language="JavaScript">
function kH(e)
{
 var pK = e ? e.which : window.event.keyCode;
 return pK != 13;
}

function DisableEnterAndTab ()
{
 if (document.all) // IE
  {
  document.onkeypress = kH;
  }
 if (document.layers)  
  {
  document.captureEvents(Event.KEYPRESS);
  }
}
</SCRIPT>

How do I incorporate the Tab key?

Thanks
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16282805
return pK != 13 && pK != 9;
0
 
LVL 29

Accepted Solution

by:
Pravin Asar earned 2000 total points
ID: 16284124
Here is updated code, does detailed browser check and disable  tab key.

Tested with IE 6.XX, FF 1.5X, NS8+

<html>
<head>
<title>Disable Enter and Text </title>

<script language="javascript">
function BrowserType () {
      var srchText = navigator.userAgent;
      var brwTypes = ("Opera,MSIE,Netscape,Firefox").split(',');
      for (var ix=0; ix < brwTypes.length; ix++) {
            if (srchText.toString().match(brwTypes[ix])) {
                  return brwTypes[ix];
            }
      }
      return null;
}
var brwType = BrowserType();
function doAction (evt) {
      return false;
      if (evt) {
            evt.returnValue = false;
            evt.cancelBubble = true;
      }
      else {
            alert ('Bad Event Object');
      }
}

function kH(e) {
      evt = (e) ? e : window.event;
      var type = evt.type;
      var pK = e ? e.which : window.event.keyCode;
      if (pK == 9) { pK = 13; evt.keyCode = 13; }
      if (pK == 13) {
             doAction (evt);
             return false;
      }
      if (pK == 9 || pK == 0) {
            doAction (evt);
            return  false;
      }
}

function DisableEnterAndTab ()
{
      if (!brwType) { return; }
      if (brwType == 'MSIE') {
            document.onkeypress = kH;
            document.onkeydown = kH;
      }
      else if (brwType == 'Firefox') {
            document.onkeypress = kH;
      }
      else if (brwType == 'Netscape') {
            document.onkeypress = kH;
            if (document.captureEvents) {
            document.captureEvents (Event.KEYPRESS);
            }
      }
      else {
            alert ('UnSupported Browswer');
      }
}
DisableEnterAndTab();
</script>
</head>

<body>
<form name="MyForm" onSubmit="alert('No you did not disable them'); return;">
<br>
<input type="text" name="t1" value="">
<br>
<input type="text" name="t2" value="">
<br>
<input name="TheText">
<br>
<input type="submit">
</form>

</body>
</html>

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16284546
Hehe.. Just needed a little nudge

Michel
0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 16284660
Michel

Thanks for movitating me to get a better solution.

My earlier solution does not work in all browser.

Please try it yourself

0
 
LVL 1

Author Comment

by:indyng
ID: 16285359
return pK != 13 && pK != 9;

This is not working. Thanks
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16288083
Pravinsar: I think this only works in IE: evt.keyCode = 13;
0
 
LVL 1

Author Comment

by:indyng
ID: 16288364
mplungjan: What you gave me - "return pK != 13 && pK != 9;" only works for Enter key and not Tab.

Thanks
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16288545
Possible. Try Pravinsar's script.
0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 16289429
Trying to catch tab event is a tricky thing. First I thought the code posted earlier would work. Hence a new version.

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question