Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


prevent enter key from submitting form

Posted on 2014-01-29
Medium Priority
Last Modified: 2014-01-29
I'm trying to prevent default actions from occurring when the Enter key is pressed. I'm working in someone else's .NET form. I've found a couple of jquery options intended to defeat the Enter key form submission, but neither is working for me. Here's what I've tried:

$("input").keypress(function (evt) {
var charCode = evt.charCode || evt.keyCode;
if (charCode  == 13) { 
return false;

  $(document).ready(function() {
		$(document).keypress(function(e) {
			if(e.keyCode == 13) e.preventDefault();

Open in new window

Here you can see what I'm trying to defeat:

<input name="ctl00$ctl00$cntrlID_11078" type="text" value="" id="ctl00_ctl00_cntrlID_11078" onkeypress="if(typeof(CheckForFormSubmit)==&#39;function&#39;){ return CheckForFormSubmit(event);}" />

Open in new window

Who can help?
Question by:GessWurker
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 82

Accepted Solution

leakim971 earned 2000 total points
ID: 39818674
Without the following code :
With the following code :
$(document).ready(function() {
    $(":text").each(function() {
        $(this).data("keypress", $(this).attr("onkeypress"));
   $(document).keypress(function(e) {
	if(e.keyCode == 13) {
        else if($("keypress")) {
            var toRun = new Function($("keypress"));

Open in new window

Because the inline onkeypress will ALWAYS run first :(, we remove it, save it, break it, smash it, concatenate it to run it only if it's not the ENTER key pressed

Author Comment

ID: 39818787
Perfect, leakim971. Thanks!!!

Author Comment

ID: 39818840
Oops. I found an issue. In a form that also contains textareas, we can't enter line-breaks. Can the code be modified so that it allows the Enter key to work in a textarea?

Author Comment

ID: 39818871
Strike that. I see that this does exactly what I need:

var CheckForFormSubmit = function() {


Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question