Posts Tagged ‘javascript detect enterkey event’

This code is useful in almost all kind of forms that has a submit button. When any user press “Enter key” also called a Return key in a textbox, this code will detect if the Enter key is pressed or not and will perform the same action as directed in a code of Submit button. Type the following javascript code befor a “body” tag.

function DetectEnterPressed(e) {
var characterCode
if(e && e.which){ // NN4 specific code
e = e
characterCode = e.which
else {
e = event
characterCode = e.keyCode // IE specific code
if (characterCode == 13) return true // Enter key is 13
else return false

Type the following line of code in a code behind file. You can put this code in a page_load method and outside the IsPostBack event.

txtPassword.Attributes.Add("onkeypress","javascript:return DetectEnterPressed(event);");