Results 1 to 4 of 4

Thread: cancelling a submit

  1. #1

    Thread Starter
    Hyperactive Member richy's Avatar
    Join Date
    Jan 1999
    Location
    Liverpool, England
    Posts
    353
    Hopefully a nice easy question. I have a form, but when the user presses enter on a textbox, the form submits, which I do not want it to do. I have the following code, but don't know how to cancel the form from submitting.

    JAVASCRIPT

    function textchange(thekey)
    {
    if(thekey==13)
    {
    This is where the code should go I believe, that would cancel the submission.
    }
    }


    HTML

    <input SIZE=10 type='text' name='thedate' onKeypress="textchange(event.keyCode);'"></TD></TR>

  2. #2
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810

    Thumbs up

    From MSDN
    If you have data dependencies between fields, using the onkeyup or onchange events is a good way to make sure data is valid before moving on. On the other hand, these techniques have more code maintenance issues compared with the efficient (from a coding perspective) global approach of validating everything on form submission. To do this you just need to handle the onsubmit event on the FORM element. If you are unable to validate the form data, you can cancel submission by returning false from your event handler function. Returning true allows the submit operation to continue (you'll need to indicate the function correctly, as shown in the FORM element MSDN documentation).
    Code:
    <BODY>
    <FORM NAME="oDoSubmit" onsubmit="return(myOnSubmitEventHandler());">
    </FORM>
    </BODY>
    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: [email protected]
    Working towards a bug free world......
    (Not a Microsoft employee)

  3. #3

    Thread Starter
    Hyperactive Member richy's Avatar
    Join Date
    Jan 1999
    Location
    Liverpool, England
    Posts
    353
    What I don't understand is in this event handler, how would I then check whether the submit button had been clicked, or as to whether the button had had enter pressed on it.

  4. #4

    Thread Starter
    Hyperactive Member richy's Avatar
    Join Date
    Jan 1999
    Location
    Liverpool, England
    Posts
    353
    Thanks Jerry. You got me thinking and I've now got it sorted.
    I had a function in JavaScript before to check all the details on the form.
    On my keypress of my textbox I set a value to "bad" if it was the keypress of enter.
    On my checks, if this value equalled bad, then to cancel the submit.

    simple really!!!

Posting Permissions

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



Click Here to Expand Forum to Full Width