|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
SitePoint Member
Join Date: Sep 2003
Location: preston
Posts: 11
|
ensure validation upon pressing enter
Hi Peeps,
I have a standard form using javascript to validate and edit the results using the code "onSubmit=validateFunc()". This is great and works fine if the user clicks the submit button but if they just press enter it doesnt call the function. is there any way i can force the function to be called every time? ianio |
|
|
|
|
|
#2 |
|
Drupaler
![]() Join Date: Jul 2002
Location: London, UK
Posts: 3,299
|
you could combine it with a key capture... or remove your button and replace it with a text link like this one and make your validation part of the submitForm() function.
http://www.javascript-coder.com/java...m-submit.phtml |
|
|
|
|
|
#3 |
|
SitePoint Member
Join Date: Sep 2003
Location: preston
Posts: 11
|
I thought about the key capture idea but what is the enter key called?
i assume i use an onKeyDown function to see if the enter key is pressed and then submit the form if this is true. |
|
|
|
|
|
#4 |
|
Drupaler
![]() Join Date: Jul 2002
Location: London, UK
Posts: 3,299
|
Google:
http://www.google.co.uk/search?hl=en...re+codes&meta= leads to: http://www.htmlgoodies.com/beyond/keycapture.html Enter = 13 and full cross-browser script. G |
|
|
|
|
|
#5 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Apr 2004
Location: germany
Posts: 4,321
|
Hmm... form.onsubmit always fires when form is being submitted, regardless of which cause (enter key, click or click() event). The only exception is form.submit() method.
So, instead of catching keyboard events (which is completely irrelevant here) make sure that you use a correct syntax Code:
<form ... onsubmit="return validateFunc()"> |
|
|
|
|
|
#6 | |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Aug 2004
Location: Chicago
Posts: 255
|
Quote:
No reason to complicate your pages for functionality that is built into the browser anyway... Charles |
|
|
|
|
|
|
#7 |
|
SitePoint Member
Join Date: Sep 2003
Location: preston
Posts: 11
|
Ooh so close.
this is the simple version of the code i used (Thanks to Greg.Harvey) <html> <head> <script language="JavaScript"> <!-- function whatButton() { if (window.event.keyCode == 13) { alert('Thats the enter key'); } } // --> </script> </head> <body onKeyPress="whatButton()"> <p>Press a key</p> </body> </html> When the function realised you pressed enter i told it to call the standard Validate() function. This it did and i got an alert saying "there are fields you havent filled in" but it then continued to submit the form anyway which led to the same old error message. There must be a way to say "Do not submit when the enter button is pressed" |
|
|
|
|
|
#8 |
|
SitePoint Member
Join Date: Sep 2003
Location: preston
Posts: 11
|
Ah. i had a <input type="button" onclick="Validate()">
using the <input type="submit"> and onsubmit="return Validate()" seems to have done the job. Thankyou ALL |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 16:59.










Hybrid Mode
