473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Preventing the Enter key from submitting a form

Hi,

I have the following function that check for the <Enterkey being
pressed on an <Inputfield:

function disableCR(e) {
if (window.event) {
key = window.event.keyCode;
} else {
key = e.which;
}

alert(key);
if (key == 13) {
alert('false');
return false;
} else {
alert('true');
return true;
}
}

My previous HTML was like so:
<input type="text" name="total" value="100" size="6" maxlength="4"
onkeypress="return disableCR(event)">

I would like to convert this into an Event Listener using DOM but not
sure how to do this. Has anyone done this?

Nov 5 '07 #1
2 1361
donpro wrote:
I have the following function that check for the <Enterkey being
pressed on an <Inputfield:

function disableCR(e) {
if (window.event) {
key = window.event.keyCode;
} else {
key = e.which;
}
[...]
}

My previous HTML was like so:
<input type="text" name="total" value="100" size="6" maxlength="4"
`type="text"' is redundant as that is the default value for that attribute.
onkeypress="return disableCR(event)">

I would like to convert this into an Event Listener using DOM but not
sure how to do this. Has anyone done this?
You are already using a event listener, and you are using the DOM; in fact,
you are supporting at least three DOMs: the MSHTML-DOM with window.event,
the W3C-DOM and the NN4/Gecko-DOM with `e', and the NN4/Gecko-DOM with
`e.which'.

The local `event' property in the execution context of the event listener
code is proprietary to refer to an Event object, indeed, but I have yet to
see a DOM that does not have it. Avoiding it will only lead to your
scripting branches for several DOM Event implementations, of which only
three are known.

IOW: If it ain't broken, don't fix it.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Nov 9 '07 #2
Thomas 'PointedEars' Lahn wrote:
donpro wrote:
><input type="text" name="total" value="100" size="6" maxlength="4"

`type="text"' is redundant as that is the default value for that attribute.
I would rather advocate the following:

| This is not a required attribute, but we think you should
| include it. If omitted, IE 5.5 will still display a text field,
| but Netscape 4.7 will not.

http://www.w3schools.com/tags/tag_input.asp

--
Bart

Nov 9 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bruce | last post by:
I have a number of forms that do significant work based on variables POSTed from the form. What is the common method of detecting and preventing this work from being done when the form is POSTed as...
15
by: M Smith | last post by:
I have a form I want to submit to itself. I want to be able to type in a list of numbers and submit the form and have that list show up on the same form under the text box I typed them into and...
8
by: CJM | last post by:
How do people go about preventing the user from submitting a form for a 2nd time? For example, the user submits a form, clicks on the back button, and the submits the form again. I have used...
2
by: Astra | last post by:
Hi All In order for me to validate forms before they are submitted I use an INPUT type of button and fire the OnClick event to check the form before submitting. This is instead of the usual...
4
by: Simon Wigzell | last post by:
Is there a way to prevent a form submitting when you press enter on a text field? Some people press enter when they finish typing in a text field out of habit I guess unconcsciously thinking it...
4
by: roxanaislam | last post by:
Submitting Form by pressing the "ENTER" key -------------------------------------------------------------------------------- Hi: I have a search form in my application which has 4 dropdown...
2
by: mikemiller.innova | last post by:
I developed a C# Windows App that requires a user to input their real name and what they did to the server while they were online. I am going to launch the app att shutdown via a logoff script. ...
4
by: nkoier | last post by:
Hi, I've been going crazy trying to figure out what's wrong with our Asp.Net 2.0 intranet site. At the very top of our main page I provide a TextBox and a Button for submitting Google searches....
2
by: dwmartin18 | last post by:
I got it into my head the other day to develop my own little form validation library. More than anything I just wanted to try out of few things I’ve never done before like chainable methods -- think...
12
by: Daniel Klein | last post by:
I'm pretty new at php and web stuff so please be gentle with me. I'm trying to get a form to submit when the user presses the Enter key. I do not want to use javascript. I've googled this to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.