Go Back   SitePoint Forums > Forum Index > Program Your Site > JavaScript
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Aug 17, 2004, 08:04   #1
ianio
SitePoint Member
 
ianio's Avatar
 
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
ianio is offline   Reply With Quote
Old Aug 17, 2004, 08:09   #2
greg.harvey
Drupaler
bronze trophy
 
greg.harvey's Avatar
 
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
greg.harvey is offline   Reply With Quote
Old Aug 17, 2004, 08:19   #3
ianio
SitePoint Member
 
ianio's Avatar
 
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.
ianio is offline   Reply With Quote
Old Aug 17, 2004, 08:43   #4
greg.harvey
Drupaler
bronze trophy
 
greg.harvey's Avatar
 
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
greg.harvey is offline   Reply With Quote
Old Aug 17, 2004, 09:03   #5
stereofrog
SitePoint Wizard
 
stereofrog's Avatar
 
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()">
and don't call form.submit directly.
stereofrog is offline   Reply With Quote
Old Aug 17, 2004, 09:35   #6
Guimauve
SitePoint Addict
 
Guimauve's Avatar
 
Join Date: Aug 2004
Location: Chicago
Posts: 255
Quote:
Originally Posted by stereofrog
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
Agree with this 100%


No reason to complicate your pages for functionality that is built into the browser anyway...

Charles
Guimauve is offline   Reply With Quote
Old Aug 17, 2004, 09:09   #7
ianio
SitePoint Member
 
ianio's Avatar
 
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"
ianio is offline   Reply With Quote
Old Aug 17, 2004, 09:31   #8
ianio
SitePoint Member
 
ianio's Avatar
 
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
ianio is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 16:59.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved