Changing action of key in text input controls

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I noticed that in IE9 if I entered data in a text box in a form and hit =
<Enter> key, it would cause an error sound and not act as I expected, like=20
the <Tab> key, to advance to the next control and activate the onChange=20
event to update other controls. It worked OK in Firefox but not IE9. =

So I found some references:

The second reference used a rather convoluted way to achieve the desired =

effect. But the first reference gave me an idea:

  <script type=3D"text/javascript">
  function stopRKey(evt) {
    var evt = (evt) ? evt : ((event) ? event : null);
    var node = ( ? : ((evt.srcElement) ?=20
evt.srcElement : null);
    if ((evt.keyCode =3D 13) && (node.type=3D=3D"text"))
  //    window.postMessage(String.fromCharCode(9), window.location);
      return false;}

  document.onkeypress = stopRKey;


I tried to simulate the sending of the <Tab> character to replace the =
character, but that didn't work. However, the blur() works well enough=20
except it removes focus and you have to click on the next control.

I have implemented this here:

Any ideas on a better way to do this?



Re: Changing action of key in text input controls


     You might have done better to post to comp.lang.javascript as
your problem is not an HTML one.

Quoted text here. Click to load it

     It is trying to submit the form.


Quoted text here. Click to load it

     I have my input controls' onkeydown clause
          onkeydown="return DoKeydownInput(event);"
The code following is very much tied to my special form handler, but
as you might share some of my concerns (such as <Enter> still being
wanted in a textara control, I left it all in.

     Caveat: I am not sure that my event handling works properly
outside of IE.  If you correct that part, please let me know what you
did and why.

***** Start of Code *****
// DoKeydownInput()
// Handle onkeydown of Input Controls
// Last Modification: 2012-03-27
// Do not modify any assignments to fBypassValidation in this method
// first reading the note under <Esc> handling in this method.

function DoKeydownInput
 theEvent     // event: event that fired (assumed to be keyboard
  var theKey=theEvent.keyCode;

  // <Enter>
  // Do not allow <Enter> through on a text control as it would submit
  // form.  With a textarea control, this does not happen -- <Enter>
  // a line break, so allow it.
  if (theKey==13)     // Kludge: hardcoded value: <Enter> keycode
    switch (FieldData[].ControlType)
    case "text":
       "Use the "+Gebi("btnSubmit").value+
       " button instead to submit the form.");
       // not a debugging alert()
      return false;
    case "textarea":
      return true;
      // Do nothing.

  // <Esc>
  // The logic to implement form resetting here can run afoul of the
  // validation code with events that I have not been able to trace.
  // fBypassValidation=false after doing the reset seems to
  // trigger validation.  However, I have hit on a solution.  I do not
  // fBypassValidation in this handling.  This might appear to disable
  // validation afterwards, but this method is called frequently and
  // importantly, before any other validation would be executed.  If
the next
  // character is <Enter>, it will be processed and fBypassValidation
  // If the next character is <Esc>, the situation with
  // simply continues.  If the next character is any other character,
  // fBypassValidation is set to false, and no validation gets
  // Success!
  if (theKey==27)     // Kludge: hardcoded value: <Esc> keycode
    return false;

  // Any other key
  return true;
***** End of Code *****


Gene Wirchenko

Site Timeline