Solved

prevent enter key from submitting form

Posted on 2014-01-29
4
597 Views
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?
0
Comment
Question by:GessWurker
  • 3
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39818674
Without the following code : http://jsfiddle.net/TkbXK/
With the following code : http://jsfiddle.net/TkbXK/1/
 
$(document).ready(function() {
    $(":text").each(function() {
        $(this).data("keypress", $(this).attr("onkeypress"));
        $(this).removeAttr("onkeypress");
    });
   $(document).keypress(function(e) {
	if(e.keyCode == 13) {
            e.preventDefault();
        }
        else if($(e.target).data("keypress")) {
            var toRun = new Function($(e.target).data("keypress"));
            toRun();
        }
   });
});

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
0
 

Author Comment

by:GessWurker
ID: 39818787
Perfect, leakim971. Thanks!!!
0
 

Author Comment

by:GessWurker
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?
0
 

Author Comment

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

var CheckForFormSubmit = function() {
     javascript:void(0);
}

Thanks!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now