• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 625
  • Last Modified:

prevent enter key from submitting form

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
GessWurker
Asked:
GessWurker
  • 3
1 Solution
 
leakim971PluritechnicianCommented:
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
 
GessWurkerAuthor Commented:
Perfect, leakim971. Thanks!!!
0
 
GessWurkerAuthor Commented:
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
 
GessWurkerAuthor Commented:
Strike that. I see that this does exactly what I need:

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

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now