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;
}
}