Press Enter on a text box

Discussion in 'Javascript' started by JCO, Feb 12, 2004.

  1. JCO

    JCO Guest

    How's come when I press the enter key, I can't get it to execute the correct
    password.
    It seems that I'm forced to press the button. I want to be able to do both.

    How is this done?
     
    JCO, Feb 12, 2004
    #1
    1. Advertising

  2. JCO

    kaeli Guest

    In article <k0RWb.1679$>,
    enlightened us with...
    > How's come when I press the enter key, I can't get it to execute the correct
    > password.
    > It seems that I'm forced to press the button. I want to be able to do both.
    >



    You're going to have be a lot more specific.
    Got a url?

    --
    --
    ~kaeli~
    A midget fortune teller who escapes from prison is a small
    medium at large.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
     
    kaeli, Feb 12, 2004
    #2
    1. Advertising

  3. JCO

    JCO Guest

    Sorry for the lack of detail. Site is not hosted at this time.
    I will try again. I have a logon form (logon.htm) that contains a Textbox,
    Push Button (label = Enter Password) & and a Clear button (label is clear).
    Simple as that.

    Currently, you would enter the password in the text box and select the
    button. The script does the rest. I want to modify it by allowing the user
    to type in the password and simply press the enter key as another method to
    validate the form. I tried to trap the enter key as shown, but it is not
    working.

    <FORM NAME="frmPassword" onSubmit="return valForm()">
    <input type=password name="txtInput" size="15" onkeypress="onEnter();" >
    <input type="button" value="Enter Password" name="btnEnter"
    onclick="valForm();">
    <input type="reset" value="Clear" name="btnClear"></p>

    function onEnter(){
    if(event.keyCode==13)
    document.frmPassword.btnEnter.click();
    }

    What is wrong with this?



    "kaeli" <> wrote in message
    news:...
    > In article <k0RWb.1679$>,
    > enlightened us with...
    > > How's come when I press the enter key, I can't get it to execute the

    correct
    > > password.
    > > It seems that I'm forced to press the button. I want to be able to do

    both.
    > >

    >
    >
    > You're going to have be a lot more specific.
    > Got a url?
    >
    > --
    > --
    > ~kaeli~
    > A midget fortune teller who escapes from prison is a small
    > medium at large.
    > http://www.ipwebdesign.net/wildAtHeart
    > http://www.ipwebdesign.net/kaelisSpace
    >
     
    JCO, Feb 13, 2004
    #3
  4. On Fri, 13 Feb 2004 02:29:56 GMT, JCO <> wrote:

    > <FORM NAME="frmPassword" onSubmit="return valForm()">
    > <input type=password name="txtInput" size="15" onkeypress="onEnter();" >
    > <input type="button" value="Enter Password" name="btnEnter"
    > onclick="valForm();">
    > <input type="reset" value="Clear" name="btnClear"></p>
    >
    > function onEnter(){
    > if(event.keyCode==13)
    > document.frmPassword.btnEnter.click();
    > }
    >
    > What is wrong with this?


    You don't cancel the event. Try:

    function onEnter( evt, frm ) {
    var keyCode = null;

    if( evt.which ) {
    keyCode = evt.which;
    } else if( evt.keyCode ) {
    keyCode = evt.keyCode;
    }
    if( 13 == keyCode ) {
    frm.btnEnter.click();
    return false;
    }
    return true;
    }
    ...
    <input type="password" name="txtInput" size="15"
    onkeypress="return onEnter(event,this.form);" >

    This should work (partially tested on) Opera 7.23, Netscape 7, IE 6, and
    Mozilla 1.6. Your original code could only have worked on IE and Opera;
    Netscape and Mozilla don't support a global event object, or event.keyCode.

    Mike

    --
    Michael Winter
    d (replace ".invalid" with ".uk" to reply)
     
    Michael Winter, Feb 13, 2004
    #4
  5. JCO

    JCO Guest

    It is working now. Thanks for your help.

    "Michael Winter" <> wrote in message
    news:eek:...
    > On Fri, 13 Feb 2004 02:29:56 GMT, JCO <> wrote:
    >
    > > <FORM NAME="frmPassword" onSubmit="return valForm()">
    > > <input type=password name="txtInput" size="15" onkeypress="onEnter();" >
    > > <input type="button" value="Enter Password" name="btnEnter"
    > > onclick="valForm();">
    > > <input type="reset" value="Clear" name="btnClear"></p>
    > >
    > > function onEnter(){
    > > if(event.keyCode==13)
    > > document.frmPassword.btnEnter.click();
    > > }
    > >
    > > What is wrong with this?

    >
    > You don't cancel the event. Try:
    >
    > function onEnter( evt, frm ) {
    > var keyCode = null;
    >
    > if( evt.which ) {
    > keyCode = evt.which;
    > } else if( evt.keyCode ) {
    > keyCode = evt.keyCode;
    > }
    > if( 13 == keyCode ) {
    > frm.btnEnter.click();
    > return false;
    > }
    > return true;
    > }
    > ...
    > <input type="password" name="txtInput" size="15"
    > onkeypress="return onEnter(event,this.form);" >
    >
    > This should work (partially tested on) Opera 7.23, Netscape 7, IE 6, and
    > Mozilla 1.6. Your original code could only have worked on IE and Opera;
    > Netscape and Mozilla don't support a global event object, or

    event.keyCode.
    >
    > Mike
    >
    > --
    > Michael Winter
    > d (replace ".invalid" with ".uk" to reply)
     
    JCO, Feb 13, 2004
    #5
  6. JCO

    Ivo Guest

    " JCO" <> wrote in message
    news:EEWWb.14139$...

    > <FORM NAME="frmPassword" onSubmit="return valForm()">
    > <input type=password name="txtInput" size="15" onkeypress="onEnter();" >
    > <input type="button" value="Enter Password" name="btnEnter"
    > onclick="valForm();">
    > <input type="reset" value="Clear" name="btnClear"></p>
    >
    > function onEnter(){
    > if(event.keyCode==13)
    > document.frmPassword.btnEnter.click();
    > }
    >


    I believe I noticed that the "Enter key" functionality depends on the
    presence of an <input type="submit"> in the form. I had a form where I
    replaced it with a <button> (to allow underlined letters) and the Enter key
    stopped working there and then. The non-javascript dependent solution I
    ended up with was to add an <input type="submit"> with a height and width of
    1.
    HTH
    Ivo
     
    Ivo, Feb 13, 2004
    #6
  7. JCO

    JCO Guest

    Are you saying to change the button (that says "Enter Password") should be
    of type submit or are you saying the textbox should be of type submit.

    I have changed the textbox to type=password; this makes sense.

    "Ivo" <> wrote in message
    news:402d0888$0$70750$...
    > " JCO" <> wrote in message
    > news:EEWWb.14139$...
    >
    > > <FORM NAME="frmPassword" onSubmit="return valForm()">
    > > <input type=password name="txtInput" size="15" onkeypress="onEnter();" >
    > > <input type="button" value="Enter Password" name="btnEnter"
    > > onclick="valForm();">
    > > <input type="reset" value="Clear" name="btnClear"></p>
    > >
    > > function onEnter(){
    > > if(event.keyCode==13)
    > > document.frmPassword.btnEnter.click();
    > > }
    > >

    >
    > I believe I noticed that the "Enter key" functionality depends on the
    > presence of an <input type="submit"> in the form. I had a form where I
    > replaced it with a <button> (to allow underlined letters) and the Enter

    key
    > stopped working there and then. The non-javascript dependent solution I
    > ended up with was to add an <input type="submit"> with a height and width

    of
    > 1.
    > HTH
    > Ivo
    >
    >
     
    JCO, Feb 13, 2004
    #7
  8. On Fri, 13 Feb 2004 20:47:37 GMT, JCO <> wrote:

    [Fixed top-post]

    > "Ivo" <> wrote in message
    > news:402d0888$0$70750$...
    >
    >> I believe I noticed that the "Enter key" functionality depends on the
    >> presence of an <input type="submit"> in the form.


    [snip]

    > Are you saying to change the button (that says "Enter Password") should
    > be of type submit or are you saying the textbox should be of type submit.


    What I believe Ivo is trying to say is that when a submit button is
    present in a form and the Enter key is pressed whilst a textbox or
    password field[1] in the same form has focus, the form should be submitted.

    If you change btnEnter to type submit, you might not need the script I
    presented (I wasn't really thinking about that, I just fixed your script).

    Mike

    [1] I don't remember if this extends to other form controls.

    --
    Michael Winter
    d (replace ".invalid" with ".uk" to reply)
     
    Michael Winter, Feb 14, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Natty Gur
    Replies:
    0
    Views:
    582
    Natty Gur
    Jun 30, 2003
  2. w. jORDAN
    Replies:
    3
    Views:
    3,739
    w. jORDAN
    Jul 22, 2003
  3. csgraham74

    Enter Press on Text box ASP.Net

    csgraham74, Sep 27, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    11,764
    Grant Merwitz
    Sep 27, 2005
  4. donald
    Replies:
    1
    Views:
    32,652
    donald
    May 16, 2006
  5. HopfZ
    Replies:
    0
    Views:
    576
    HopfZ
    Aug 28, 2006
Loading...

Share This Page