SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Dec 2002
    Location
    Philadelphia
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Disabling the automatic submission of forms with Enter or Return button

    This is a client side issue, so I though I'd put it in the Javascript forum. If it really belongs on the HTML forum, I'm open to getting it moved.

    I have several text fields in a form, and many of them seem to invoke the hitting of the "Enter" key on behalf of the user, which prematurely submits the form.

    Is there any way to disable the Enter button from submitting the form automatically? Thanks.

    By the way, I tried this method:

    <script type="text/javascript">
    function noenter() {
    return !(window.event && window.event.keyCode == 13); }
    </script>

    and putting this code into the text field code:
    onkeypress="return noenter()"


    and it works ONLY in Internet Explorer, does not work in Firefox.


  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you disable the enter key then no one will be able to submit the form who doesn't have a mouse. You will do much better to get rid of the scripts that are automatically submitting the form prematurely and code it correctly so that all the validation is done from the form onsubmit where it will be run at the correct time.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Addict
    Join Date
    Dec 2002
    Location
    Philadelphia
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The users who will be using this form are a limited group. It's not a form for the public. This form will be used by a set number of teachers all working in a lab with the same hardware.

    I certainly understand the usability issues you mentioned but those wouldn't apply in this case.

    I just need to disable the enter key on all browsers, including Firefox.

    thanks

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Browser owners have control over that functionality in decent web browsers and so the ability of web pages to block people from using the functionality that they want is limited. When you attach the validation to the form onsubmit then it doesn't matter how they submit the form it still performs the validation and blocks the submit if the form isn't filled out correctly. Validating there will make it unnecessary to try to block the enter key which you can't do unless your visitors allow it anyway.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Addict
    Join Date
    Dec 2002
    Location
    Philadelphia
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, as I think of it , you're right.

    Part of what I was struggling with is that the form has dynamically generated javascript components that the user creates as it goes along. For each phase of the process, the user determines how many questions they want to create for a test for the children.

    So I balked at the idea of validation at the beginning, because the number of user generated form elements is actually totally determined by the teacher filling out the form. Some forms will be five pages long. Some other forms will be very very short.

    But I can still use validation. I can simply put a checkbox at the end that says "I'm done building this form" and have that validated.

    So..............validation it is. Thank you so much for helping me to think through this.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •