Home > General Interest, Programming > Disabling the Return Key in Forms

Disabling the Return Key in Forms

February 15th, 2007 ^Lestat Leave a comment Go to comments

I had a few forms that included a delete button, and also a submit button together. Having filled out forms myself I have a habit of hitting the return key. I dug up an old form I made a few years ago and found this bit of javascript to disable the key. It doesn’t disable it 100%, but only when it doesn’t have focus. So it can still be tabbed to and then the return key will work. A mouse click will do at any time…

<html>

   <head>

   <!-- disable return key -->

   <script language="JavaScript" type="text/javascript">

	   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;

   </script>

   </head>

<body>...

</body>

</html>

javascript, forms, return key

Categories: General Interest, Programming Tags:
  1. No comments yet.
  1. No trackbacks yet.