Classic ASP - Submit Form when user uses Enter Key

Hi Experts,

I've come a long way in scripting and classic ASP, but this one thing is eluding me quite nicely and I can't seem to find exactly what I need and what would work for me.

My web form is just that a standard HTML form that calls an asp page for validation.  Everything works great if you mouse click the submit button, but if you try to submit the form by clicking enter... no luck.

Here's the catch:  The page has 3 forms on it... Each form has it's own set of submit buttons... so when I click enter... I'm actually submitting the 2nd of the 3 forms called upon.... not the form where the user just typed in values.

So.. I gues what I'm asking is how do I set it up so that when a user hits the ENTER key, the form he is currently in gets submitted?  The form he is currently in will be the one where he's actually filling in text input boxes.

Setup: IIS 6 on 2k3 ASP scripts & executables
98% of scripts written if VBScript
2% of scripts are javascript

Appreciate the help!
Who is Participating?
BanthorConnect With a Mentor Commented:
ASP has completed it's part of the process before the browser starts to render.
This is an HTML issue.
The behavior you are trying to accomplish is what should be happening normally. IF there is only one button as a type="submit"
if you have other buttons in a form they should be either type="button" or type="reset"
if your user is entering data in a textarea, the Enter Key behavior is modified. If you want a user to right a several lines of data and then use enter to post the form. change all textareas to <input type="text"
you can then set the attribute lines="3" without affecting the beahvior of the enter key
Om PrakashConnect With a Mentor Commented:
Please check the following example:
function Login()
if (document.login.username.value =="")
		alert("Login Invalid - try again."); 
// End -->

<form name="login" onSubmit="Login(); return false;" action="#">
  Username <input type="text" name="username"><br/>
  <input type="submit" value="Login">

Open in new window

RanidaeAuthor Commented:
K.. when I click enter, it submits, but it doesn't gather all the fields in the form, only the one field.  How does this get modified to cause  all input fields to be submitted along with the "username" field.
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Om PrakashCommented:
Above example is only for one field. you can include all the fields you need.
scrathcyboyConnect With a Mentor Commented:
Maybe a simpler way with these 3 forms is to block ENTER from submitting any form.  That way the user has to click on one or the other of the submit buttons -- a normal expectation for most users.  To see how to block enter from submitting any form, look at these --

They all show different ways of doing the same thing.
Lines is not correct, might have to do that with style.
RanidaeAuthor Commented:
Well, I found an answer outside of the forum:

Java script at the top of the page (not in the <head>)
function entsub(myform) {
  if (window.event && window.event.keyCode == 13)
    return true;}

and in every form element (type="text" type="radio" ... ) I add: onkeypress="return entsub(this.form)"

and presto... fixed all my problems.

Thanks all for your contributions.

RanidaeAuthor Commented:
A solution was independently found outside of this posting.  Points assigned according to the lvl of learning provided by each expert.
any (or most) of us could have told you the javascript check on CR -- but you wanted an ASP solution, as the question was asked.
RanidaeAuthor Commented:
My appologies for not broadening the options.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.