| re: Using Enter Key to submit a form with HTC and onblur event
Peloux wrote:[color=blue]
> 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.[/color]
OK, so let's go back to your original post:
Peloux wrote:[color=blue]
> Hi,
>
> I have written some htc in order to validate data in a form.[/color]
"htc"? Or JavaScript (JS)? Sorry, haven't come across "htc"
before.
[color=blue]
> 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>[/color]
use:
<script type="text/javascript">
the "type" attribute is required.
[color=blue]
> function touche_EnterKeyPress(){
> if (window.event.keyCode==13){
> maForm.submit()[/color]
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();
[color=blue]
> }
> }
> </SCRIPT>
> <BODY onkeydown="javascript:EnterKeyPress();">[/color]
No need for the "javascript" pseudo-protocol, just use:
<BODY onkeydown="EnterKeyPress();">
You are calling "EnterKeyPress" but your function is called
"touche_EnterKeyPress".
[color=blue]
> -----------
> The problem is that the field of the form are not validated
> because the onblur event seems to be fire after the submit
> event ![/color]
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 |