How to disable form submission on Enter key press using jQuery

There are times that you do not want a form to automatically submit when a user hits the enter key. Or if you want to do some validation via javascript before you allow the submit to go through.

$(form).keypress(function (e) {  	
	var charCode = e.charCode || e.keyCode || e.which;
	if (charCode  == 13) {
		return false;
  	}
});

By returning false in the keypress function it tells the browser not to allow the enter key event.

Advertisements

1 thought on “How to disable form submission on Enter key press using jQuery

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s