Enter key and web forms: seeking an explanation of the behaviour described

I do know that if a user presses the enter key on a form that contains even a single input field, the browser will submit the form. And, I also know that if you would like to validate the form submission on an enter key press you need to trap the keypress by writing some JavaScript. However, I am encountering a couple of behaviours at my end, which I am not able to fully comprehend. I have inherited a web application. There are a few forms in that application that elicit end user input when launched on a browser. The forms are mostly alike; they have an input field, a dialog list field and a few text areas. Now, for a couple of forms, when accessed over the web, if the enter key is pressed the forms are not submitted. For the remainder of the forms, the pressing of the enter key submits the form. I spent the entire day, yesterday, reading the posts in this forum, and checking the source HTML and JavaScript of those forms. There is no JavaScript in the form that is trapping the enter key press event and their HTML is, more or less, exactly the same. The form properties are all the same. Everything is identical between the forms. However, as I have mentioned before, for a couple of them the browser does not submit them when the enter key is pressed, for the rest it does. I am unable to figure out what exactly is specified in those forms that is preventing them from being submitted when the enter key is pressed. As I have said, there is no JavaScript trapping the event to prevent the submission. Is there anything that you can specify at a form level using HTML that prevents this default behaviour? Any explanation about this behaviour will be greatly appreciated. And if this is a silly, very basic question, please pardon this newbie.

The second behaviour that has me flummoxed is the trapping of the enter event. I am trying to trap the enter event so that I can run some validations before the form is submitted. Now, I have tried a couple of freely available JavaScript functions to do the same. Both of them are listed below. The problem is that the event is trapped, but, just once, and if the fields are empty the user is prompted with the validation failure message. However, if the user closes the message box and presses the enter key again, the form is submitted. The field validation function has been defined in JSHeader event of the form. I even tried moving the validation function within the onKeyPress event of the form, but the form is still submitted on the second enter key press. I tried putting the code in the onKeyDown and onKeyUp events, too, but, still, the forms gets submitted on the second key press. I do not know where I am going wrong, but I am just so lost. The only thing that works without failure is if I trap the event and do no further processing by saying ‘return false’. But this is not what I would like to be the result. I would like the form to be submitted and the validation function triggered on enter key press. And I would like that to happen every time, not just once. Once again, if anyone could provide me with an explanation about this aberrant behaviour, I would really appreciate it. I am at my wit’s end on this. A pointer in the right direction will be received with great admiration. Thank you.

function checkForReturn(e){

var key;



if(window.event) {

	key = window.event.keyCode;     //IE

} else {

	key = e.which;     //firefox

}    

if (key == 13) {

	var mybutton=window.document.getElementById('Submit');

	mybutton.click();	

	return false;

}

}

Subject: Enter key and web forms: seeking an explanation of the behaviour described.

The default behaviour does not submit a document when the enter key is pressed. There are text areas and rich text fields where the user may wish to use the enter key to space out content. It sounds like this functionality has been built in to the form by the developer.

One possible cause is that if a submit button on the form has focus then pressing the enter key will have the same effect as actually clicking the button.

The reason that your validation fails using the fields onKeyPress event is that initially your field has focus which prompts the validation when the user presses the enter key. If validation fails then you are moving the focus using mybutton.click(). The user then presses enter again and because your field no longer has focus the event does not fire again.

One way to validate a document is to put the validate function in your JSHeader and then in your submit button call validate().

function validate() {

var f = document.forms[0];

if (f.FieldName.value == “”) {

alert(“Error. Field is mandatory”);

f.FieldName.focus();

return false;

}

//rest of validation

//validation successful

f.submit();

}

Subject: RE: Enter key and web forms: seeking an explanation of the behaviour described.

Matt,

Thanks for your reply. Appreciate you taking the time. After reading materials available on the web and postings in this forum, I got the impression that there are default browser behaviours with regards to the enter key being pressed on a form. According to the documents, IE, by default, will submit the form. Firefox will often beep at you. I am including the URLs that provide that information for your perusal.

http://www.htmlcodetutorial.com/forms/index_famsupp_157.html

http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/bafa5c734ba13ddc85256fcd005ff392?OpenDocument

The browser in use, in our case, IE, behaves differently with different forms. These forms are, almost, identical with regards to their properties, their HTML and JavaScript code. The difference is in their layout description, an extra table on one form or an extra row and an extra column on the other. Other than that, they are the same. So, why would they be behaving differently. I have checked and rechecked the forms, they do not have any JavaScript that traps the enter event and takes action on it. I am the one who has been entrusted to take care of the enter key, and I noticed the differences while learning about the application features.

With regards to my validation failing, the JS Validation routine is in the JSHeader event of the form. I coded both the form’s onKeyPress and the field’s onKeyPress event. In both the cases, the validation runs once; the second key press submits the form. In the validation routine that is defined in the JS header, if a field fails the validation, then, after the alert, the focus is returned back to the field. So, the focus should not be in the button after the initial key press and validation failure. And I have verified this, the focus does return back to the field in question. And even if the submit button has the focus, the second key press should run the validation routine once again, since the submit button makes an explicit call to the validate function. This validation does not happen, and the form submits with the required fields being empty.

Thank you!

Subject: RE: Enter key and web forms: seeking an explanation of the behaviour described.

This may answer your question as to why your forms act differently. I can’t remember ever using only one button on a form so have not encountered this behaviour before.

http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/714115da88d3c724852571460076a6f9?OpenDocument

Subject: RE: Enter key and web forms: seeking an explanation of the behaviour described.

I apologize, if I had mentioned earlier that I have a single button on those forms. Actually, I do have two buttons on those forms: a submit button and a cancel button.

Thanks for the link. I appreciate your responses.

Subject: RE: Enter key and web forms: seeking an explanation of the behaviour described.

I found this on the notes 5 forum which may be the cause of the problem. I have tested it out and the document submits if there is only one text field on the form, the focus is in the text field and the user presses the enter button.

http://www-10.lotus.com/ldd/46dom.nsf/55c38d716d632d9b8525689b005ba1c0/abc3bef64db9754e852566a20043f25e?OpenDocument

You could validate the document in the onsubmit event as long as you don’t have any refresh formulas on the document.

tmp = validate();

if (tmp == true) {

return true;

} else {

return false;

}

Subject: RE: Enter key and web forms: seeking an explanation of the behaviour described.

Matt,

That’s exactly what it is. The forms only differ with regards to the number of text fields they contain. The ones that submit have just a single text field, the rests have more than one. Now, who would have thunk of that! I don’t know how many people are aware of this ‘feature’; and, especially, for a newbie like me it would have taken me ages to figure this out. Last Saturday, I searched everywhere, except the R5 forum, to make sense of this issue.

Thank you very much, Matt, for taking the time and responding to my questions. It is greatly appreciated. Now, that the riddle has been solved, I shall write some JavaScript to prevent this default behaviour. Once again, thanks for your time.