473,241 Members | 1,575 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,241 software developers and data experts.

Using Enter Key to submit a form with HTC and onblur event

Hi,

I have written some htc in order to validate data in a form. most of
htc are attached on 'onblur' event.

Now, we would like to use the Enter Key to sublit form, so we use the
following code :
-----------
<SCRIPT>
function touche_EnterKeyPress(){
if (window.event.keyCode==13){
maForm.submit()
}
}
</SCRIPT>
<BODY onkeydown="javascript:EnterKeyPress();">
-----------
The problem is that the field of the form are not validated because
the onblur event seems to be fire after the submit event !

If you had an idear, you're welcome, I just want to use Enter Key to
submit a form

Regards
PELOUX Olivier
Jul 23 '05 #1
4 7511
I wouldn't validate a form in onblur; onsubmit is the correct place, and
assuming you have an INPUT type="submit" element somewhere, you don't
need to intercept the enter key: the form will do it for you.

If you want to prevent a form being submitted from within onsubmit,
simply have your validation function return false.
Jul 23 '05 #2
Paul R wrote:
I wouldn't validate a form in onblur; onsubmit is the correct place, and
assuming you have an INPUT type="submit" element somewhere, you don't
need to intercept the enter key: the form will do it for you.


I'll second that. Trapping a user in a field that will not let
them quit until they get it right can cause frustration. The
field should be correct when the form is submitted, not
necessarily at every moment before that.

Also, some browsers do not lose focus on the current input field
when submit is clicked - onblur never fires, the form just
submits.

Users get used to how their browser works. If it normally does
not submit when enter is pressed, they expect it not to. A
textarea should accept "enter" key presses without submitting.
If users start to learn that their browser will submit the form
whenever they press enter, they become nervous about putting new
lines into textareas.

Having a form that sometimes submits when enter is pressed but
not other times can be really confusing for some users.

If you *really* want to validate onblur, then write the error to
the page (design it with a space say immediately adjacent to the
input for error messages when required) and make the messages
very helpful (and friendly). That way, the user gets feedback
on the validation but doesn't get an annoying alert.

If users don't want to use mouseclicks to submit the form (and
many data entry operators hate using a mouse), consider using
some other key or key combination (say alt + enter) to submit
the form from the keyboard and put the submit immediately
after the last input field in the tabindex so that once the last
field is completed, tab then enter will submit the form
(dependent on validation of course...).

Hope that all helps! :-)

--
Fred

Jul 23 '05 #3
In fact, I don't want my form to be validated by onblur event, but on
only some fields of my form. For exemple, my form contains some
input=text which represent date format, so where this field lose focus
the HTC validate that the field contains date.
Jul 23 '05 #4
Peloux wrote:
In fact, I don't want my form to be validated by onblur event, but on
only some fields of my form. For exemple, my form contains some
input=text which represent date format, so where this field lose focus
the HTC validate that the field contains date.
OK, so let's go back to your original post:

Peloux wrote: Hi,

I have written some htc in order to validate data in a form.
"htc"? Or JavaScript (JS)? Sorry, haven't come across "htc"
before.
most of htc are attached on 'onblur' event.

Now, we would like to use the Enter Key to sublit form, so we
use the following code :
-----------
<SCRIPT>
use:

<script type="text/javascript">

the "type" attribute is required.
function touche_EnterKeyPress(){
if (window.event.keyCode==13){
maForm.submit()
You may be able to address your form this way in IE (presuming
it has either a name or id of "maForm"), but not in most other
browsers. Assuming the id or name is "maForm" you should use:

document.forms['maForm'].submit();
}
}
</SCRIPT>
<BODY onkeydown="javascript:EnterKeyPress();">
No need for the "javascript" pseudo-protocol, just use:

<BODY onkeydown="EnterKeyPress();">

You are calling "EnterKeyPress" but your function is called
"touche_EnterKeyPress".

-----------
The problem is that the field of the form are not validated
because the onblur event seems to be fire after the submit
event !


As Paul suggested, validate on submit. That way the form is
always validated (provided JS is enabled of course).
Alternatively, (or maybe in addition) add the validation to
touche_EnterKeyPress().

function touche_EnterKeyPress(){
if (window.event.keyCode==13){

/* call validation functions */

if (passedValidation == true) {
maForm.submit();
} else {
return false;
}
}
}
You can always do validation on blur *and* submit if you want
(provided useability is given due consideration)

Have fun! :-)
--
Fred
Jul 23 '05 #5

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

Similar topics

3
by: Megha Vishwanath | last post by:
Hi, I have a struts form <html:form> in which although the focus rests on the submit button, hitting the enter key does not submit the form. I tried capturing the Enter key event and followed...
3
by: Santosh | last post by:
Hi, I have a requirement in which, I need to capture a loan amount and the amount of down payment for that loan. According to the requirement, the user is going to enter enter the loan amount and...
6
by: guoqi zheng | last post by:
In a regular html form, when user press "enter" key, the form will be submitted. However, in ASP.NET web form, a form will only be submitted (post back) when a special button is clicked. Many...
10
by: Perry van Kuppeveld | last post by:
Hi, I have a problem with formatting a table including text fields wich can contain up to 255 chars. I need a table with 3 columns: - First column 50 % over the with a rowspan of the total...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
3
by: yuelinniao | last post by:
hi, I have got a simple way to make "textarea" support "auto-submit" when pressing Ctrl+Enter, and tested under both IE and Firefox. The common old method is like this: <form name=form2>...
4
by: Goofy | last post by:
Hi everyone, My question is related to making a form submit using javascript. Here is my scenario. I have a form, which includes a user control. The user control has a search button and a...
3
by: itp | last post by:
I have moved to Matt Kruse's Javascript Toolbox. It has some great examples to get you going quickly! Unfortunately all the examples are based on a form/submit model. I would like to try using...
6
by: Mark B | last post by:
I have a function that looks up a SQL table to see if a search term matches. It works fine but so far there are two things yet to work: 1) After entering a search term and pressing Enter, nothing...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.