[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Enter key to respond in a text box

I want a text box which is password type should be able to respond an enter key i.e; on pressing the enter key another page needs to be called. I am also not able to trap the ASCII values  of keys pressed from the keyboard in Javascript.
0
PSV
Asked:
PSV
  • 3
  • 3
1 Solution
 
Michel PlungjanIT ExpertCommented:
<html>
<head>
<script>
NS = (document.layers);
IE = (document.all);
function f1(e) { // This takes over the enter key in the specified field
   if((NS && e.which == 13) || (IE && window.event.keyCode == 13)) {
      document.myForm.submit();
      return false; // cancel the enter (Dings in Netscape)
   }
}
</script>
</head>
<BODY>
Here is a simple code for submitting a form with multiple fields.
The form is submitted by hitting enter in field 1 by assigning a fuction to the
document.form.field.onKeypress.
<FORM name=myForm action="enter.html">
1.<INPUT TYPE="PASSWORD" NAME="FirstField"  size=40><BR>
<BR>2.<INPUT TYPE="text" NAME="SecondField"  size=40>
</FORM>
<SCRIPT LANGUAGE="JavaScript1.1">
   document.myForm.FirstField.onKeyPress = f1;
</script>

<P>use document.onkeypress = keyHandler; for MSIE
and window.captureEvents(Event.KEYPRESS) with window.onkeypress  for netscape
to get all keypresses (and for example cancel enter unless the last field has the focus)
</body>
</html>


0
 
rafisternCommented:
If you make a form with only one button then an <Enter> will submit the form:

<form action="whatever">
<input type="password" name="password" size=10>
<input type="submit" value="OK">
</form>
0
 
rafisternCommented:
My solution will work in any browser, although it wasn't really what you asked.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Michel PlungjanIT ExpertCommented:
PS: A form with a single text/password field will submit with the enter in most browsers without the JavaScript:
<FORM...>
<INPUT TYPE="PASSWORD">
</FORM>

but IE3 (last time I looked) will not submit the form if there is one more entry field on the page (even if it is not in the same form)

All the 'normal' keys are passed in e.which or window.event.keyCode and can be trapped. The 13 is the enter key
0
 
Michel PlungjanIT ExpertCommented:
Rafi: Stop submitting at the same time as me ;-)

Michel
0
 
rafisternCommented:
Michel's last comment seems to me to be an optimal solution if it works. However I would prefer to have the button there for people who like clicking buttons - it makes the user interface more intuitive.

(I submit to the same threads as you because there seem not be many active experts on European timezones. Whatever is happening in the morning on our side of the world is the middle of the night in the States.)
0

Featured Post

Independent Software Vendors: 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!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now