How can I disable the Tab and Enter Key?

Hi Experts,

How can I disable the Tab and Enter Key?

Thanks
LVL 1
indyngAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kawasCommented:
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
indyngAuthor Commented:
Is it possible to disable these keys onload of the "default.asp" page rather than onkeydown?

Thanks
0
kawasCommented:
on the onload event, you would have to add the function so that onkeydown of whatever item you want the keys disabled
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

indyngAuthor Commented:
So this function is only fired when the user types?
0
kawasCommented:
yes
0
indyngAuthor Commented:
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
kawasCommented:

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
Michel PlungjanIT ExpertCommented:
@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
Pravin AsarPrincipal Systems EngineerCommented:
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
Michel PlungjanIT ExpertCommented:
0
kawasCommented:
that disables on a textfield, not the whole page
0
Michel PlungjanIT ExpertCommented:
@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
indyngAuthor Commented:
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
Michel PlungjanIT ExpertCommented:
@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
indyngAuthor Commented:
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
Michel PlungjanIT ExpertCommented:
return pK != 13 && pK != 9;
0
Pravin AsarPrincipal Systems EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michel PlungjanIT ExpertCommented:
Hehe.. Just needed a little nudge

Michel
0
Pravin AsarPrincipal Systems EngineerCommented:
Michel

Thanks for movitating me to get a better solution.

My earlier solution does not work in all browser.

Please try it yourself

0
indyngAuthor Commented:
return pK != 13 && pK != 9;

This is not working. Thanks
0
Michel PlungjanIT ExpertCommented:
Pravinsar: I think this only works in IE: evt.keyCode = 13;
0
indyngAuthor Commented:
mplungjan: What you gave me - "return pK != 13 && pK != 9;" only works for Enter key and not Tab.

Thanks
0
Michel PlungjanIT ExpertCommented:
Possible. Try Pravinsar's script.
0
Pravin AsarPrincipal Systems EngineerCommented:
Trying to catch tab event is a tricky thing. First I thought the code posted earlier would work. Hence a new version.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.