|
-
Feb 22nd, 2001, 05:15 AM
#1
Thread Starter
Hyperactive Member
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>
-
Feb 22nd, 2001, 07:13 AM
#2
Fanatic Member
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>
-
Feb 22nd, 2001, 07:23 AM
#3
Thread Starter
Hyperactive Member
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.
-
Feb 22nd, 2001, 07:40 AM
#4
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|