Disable Enter

G

Guest

How do I prevent users from submiting a form if they press the enter key. I
saw a script to disbale the enter key but I forget where. Any suggestions?
 
T

Trevor L.

Steve said:
How do I prevent users from submiting a form if they press the enter
key. I saw a script to disbale the enter key but I forget where. Any
suggestions?

This works in a field
<input type="text" id="no" value="" size="1" maxlength="3"
onkeypress="return event.keyCode!=13"/>
 
T

Trevor L.

AMysticWeb said:
Hi Steve,

http://frontpageforms.com/disable_enter.htm


Mike Smith,

http://FrontPageForms.com
FrontPage Form Tutorials
& Form Script Examples

Mike,
I made the first suggestion and you followed it up with a mroe general
function
function checkCR(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ?
evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = checkCR;

Here is something which has interested me for some time
Please explain my ignorance

Point 1. An event is set equal to a function
I take this to mean:
When the event document.onkeypress occurs, execute the function
checkCR(evt)

Point 2. A function checkCR is defined with a parameter evt that is never
passed to it
I don't undertand this.
How can the function check for the existence of evt as a passed parameter
when it is never passed ?
The first line seems to mean:
If the parameter evt exists set local variable evt equal to the value of the
parameter evt
Else
If the global variable event exists set local variable evt equal to the
value of the global variable event
Else
Set local variable evt equal to null

The second line seems to mean:
If evt.target exists set local variable node equal to evt.target
Else
If evt.srcElement exists set local variable node equal to evt.srcElement
Else
Set local variable node equal to null

I assume therefore that these are known (or may be known depending on the
browser)
evt OR event
evt.target OR event.target OR evt.srcElement OR event.srcElement
evt.keyCode OR event.keyCode
evt.target.node.type OR event.target.node.type OR
evt.targetsrcElement.node.type OR event.target.srcElement.node.type

Can you state what these entities are
I assume:
evt or event is:
some event such as keypress. What are the other possibilities?
evt.target OR event.target OR evt.srcElement OR event.srcElement is:
the element where the event takes place, e.g. an <input> tag which has a
kepypress event
evt.keyCode OR event.keyCode is:
the keyCode associated with the event (e.g. a keypress)
evt.target.node.type OR event.target.node.type OR
evt.targetsrcElement.node.type OR event.target.srcElement.node.type is:
the type of the element where the event takes place, e.g. "text" or
"textarea"

However, I note that the code does not contain any element with type="text"
Is node.type="text" a generic type which includes <input> and <textarea> ?
Any others?
 
A

Andrew Murray

A good way of preventing the form being submitted before the last field if
they press Enter in error, is to make the last field "required", so it won't
submit until they fill in the last field.

I believe there are also scripts around that make the Enter key behave like
the [Tab] key, that is it advances to the next field, then the user clicks
"Submit" rather than pressing the Enter key. Else make it plain they have
to press TAB to go to the next field, not [Enter].
 
T

Trevor L.

Andrew said:
A good way of preventing the form being submitted before the last
field if they press Enter in error, is to make the last field
"required", so it won't submit until they fill in the last field.

I believe there are also scripts around that make the Enter key
behave like the [Tab] key, that is it advances to the next field,
then the user clicks "Submit" rather than pressing the Enter key. Else
make it plain they have to press TAB to go to the next field,
not [Enter].

That sounds interesting. I am sure I have seen this behaviour on many
commercial sites.

Does anyone out there know of such a script?
I suppose it would be like the one on
http://frontpageforms.com/disable_enter.htm
but with a small change
e.g.
function checkCR(evt)
{
var evt = (evt) ? evt : ((event) ? event : null)
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement
: null)
if ((evt.keyCode == 13) && (node.type=="text"))
{ evt.keyCode = xx
// where xx is the keycode for Tab
return true } // is this correct?
}
document.onkeypress = checkCR

Any ideas, people out there
 
T

Trevor L.

Trevor said:
Does anyone out there know of such a script?
I suppose it would be like the one on
http://frontpageforms.com/disable_enter.htm
but with a small change
e.g.
function checkCR(evt)
{
var evt = (evt) ? evt : ((event) ? event : null)
var node = (evt.target) ? evt.target : ((evt.srcElement) ?
evt.srcElement
if ((evt.keyCode == 13) && (node.type=="text"))
{ evt.keyCode = xx
// where xx is the keycode for Tab
return true } // is this correct?
}
document.onkeypress = checkCR

Any ideas, people out there

In an attempt to try out my ideas. I used this code
<input type="text" id="no" value="" size="1" maxlength="3"
onkeypress="event.keyCode = (event.keyCode ==13)? 9
:event.keyCode"/>

The idea was to change 13 (Enter) to 9 (Tab) but it didn't work
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top