[Javascript] Trapping keys with NS 6

spacific at paradise.net.nz spacific at paradise.net.nz
Sat Jan 12 04:27:10 CST 2002


Hi,

I want to trap (and inhibit) various keys, such as * in a form text field.  The following code works fine in IE 5 and NS 4 (both Mac and Win), but not correctly in NS 6.  In NS 6 (again both Mac and Win versions), the * gets inserted into the field, and THEN the alert message comes up!

Any ideas of how to cope with this? Is there a new version of captureEvents that has to be set?

Many thanks,

John

------------------------------------------------------------------------------
<html> <head> <title>TEST KEYS</title>
<script language="JavaScript">
<!--
   var isNS = (navigator.appName.indexOf('Netscape') != -1);
   var isIE = (navigator.appName.indexOf('Microsoft') != -1);
      function KeyOK(e) 
      {   var _ret = true;
         if (isNS) { key=e.which} else {key=window.event.keyCode} 
         if (key == 0x2A)   // *
         {  alert("This key not allowed");
            _ret = false;
            key = 0;
         }
         return (_ret); 
      }
//--></script>
</head>
<body>
      <script language="JavaScript">
      <!--
         if (isNS) {document.captureEvents(Event.KEYPRESS)}
         document.onkeypress=KeyOK;
      // -->
      </script>
<br>
<form name="myForm">
<input type="text" name="myText"><br>
</form>
</body>
</html>
------------------------------------------------------------------------------





More information about the Javascript mailing list