Preventing the Enter key from submitting a form

Discussion in 'Javascript' started by donpro, Nov 5, 2007.

  1. donpro

    donpro Guest

    Hi,

    I have the following function that check for the <Enter> key being
    pressed on an <Input> field:

    function disableCR(e) {
    if (window.event) {
    key = window.event.keyCode;
    } else {
    key = e.which;
    }

    alert(key);
    if (key == 13) {
    alert('false');
    return false;
    } else {
    alert('true');
    return true;
    }
    }

    My previous HTML was like so:
    <input type="text" name="total" value="100" size="6" maxlength="4"
    onkeypress="return disableCR(event)">

    I would like to convert this into an Event Listener using DOM but not
    sure how to do this. Has anyone done this?
     
    donpro, Nov 5, 2007
    #1
    1. Advertising

  2. donpro wrote:
    > I have the following function that check for the <Enter> key being
    > pressed on an <Input> field:
    >
    > function disableCR(e) {
    > if (window.event) {
    > key = window.event.keyCode;
    > } else {
    > key = e.which;
    > }
    > [...]
    > }
    >
    > My previous HTML was like so:
    > <input type="text" name="total" value="100" size="6" maxlength="4"


    `type="text"' is redundant as that is the default value for that attribute.

    > onkeypress="return disableCR(event)">
    >
    > I would like to convert this into an Event Listener using DOM but not
    > sure how to do this. Has anyone done this?


    You are already using a event listener, and you are using the DOM; in fact,
    you are supporting at least three DOMs: the MSHTML-DOM with window.event,
    the W3C-DOM and the NN4/Gecko-DOM with `e', and the NN4/Gecko-DOM with
    `e.which'.

    The local `event' property in the execution context of the event listener
    code is proprietary to refer to an Event object, indeed, but I have yet to
    see a DOM that does not have it. Avoiding it will only lead to your
    scripting branches for several DOM Event implementations, of which only
    three are known.

    IOW: If it ain't broken, don't fix it.


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
     
    Thomas 'PointedEars' Lahn, Nov 9, 2007
    #2
    1. Advertising

  3. Thomas 'PointedEars' Lahn wrote:

    > donpro wrote:
    >
    >> <input type="text" name="total" value="100" size="6" maxlength="4"

    >
    > `type="text"' is redundant as that is the default value for that attribute.


    I would rather advocate the following:

    | This is not a required attribute, but we think you should
    | include it. If omitted, IE 5.5 will still display a text field,
    | but Netscape 4.7 will not.

    http://www.w3schools.com/tags/tag_input.asp

    --
    Bart
     
    Bart Van der Donck, Nov 9, 2007
    #3
    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. thechaosengine
    Replies:
    2
    Views:
    474
  2. Phil
    Replies:
    2
    Views:
    5,072
  3. BrianDH

    Enter Key H*ll, control enter key submit button

    BrianDH, Jan 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    410
    Anders Matthiessen
    Jan 12, 2005
  4. Veronica Bourke

    C# .NET stop carriage return/enter key submitting the form

    Veronica Bourke, Feb 16, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    1,188
    Veronica Bourke
    Feb 16, 2005
  5. M P
    Replies:
    1
    Views:
    494
Loading...

Share This Page