Capture Enter Key On Form Submission
keys = {
getCharCode: function(evt){
return (evt.charCode) ? evt.charCode :
((evt.which) ? evt.which : evt.keyCode);
},
isEnter:function(charCode){
return (charCode == 13);
},
submitViaEnter:function(evt) {
evt = (evt) ? evt : event;
charCode = this.getCharCode(evt);
if (this.isEnter(charCode)) {
//replace this with your own function:
form_submit();
return false;
}
return true;
}
}
To use it, add the following call to a text field:
onkeypress="return keys.submitViaEnter(event)"
This code checks if a user has pressed the Enter key after finishing typing in the input field. If the key pressed is Enter, call the function that is normally invoked on the submit event. If the key pressed is not Enter, the function returns true and a user can continue typing.

I would like to change the Enter-key to Tab-key, in order to prevent a pemature submission of the page.
I want the page to be sent only if the user really clicks the Submit button, not on Enter.
Can someone help? Thanks
Comment by Hartmut Kuwilsky — March 30, 2007 @ 8:01 pm
An extra curly brace somehow found its way into your code. Here is the corrected version:
keys = {
getCharCode: function(evt){
return (evt.charCode) ? evt.charCode :
((evt.which) ? evt.which : evt.keyCode);
},
isEnter:function(charCode){
return (charCode == 13);
},
submitViaEnter:function(evt) {
evt = (evt) ? evt : event;
charCode = this.getCharCode(evt);
if (this.isEnter(charCode)) {
//replace this with your own function:
form_submit();
return false;
}
return true;
}
}
Thanks for providing the snippet, it’s exactly what I was looking for!
Pete Hurst
Comment by Pete Hurst — April 5, 2007 @ 11:54 am
Thanks, Pete
I’ve corrected the code
Comment by Administrator — April 23, 2007 @ 7:09 pm