Sunday, 19 January 2014

jQuery Stop Form being Submitted on Enter Key Press or Disable Form Submit on Enter

Here I will explain how to stop form submit on enter in jQuery or prevent form submit on enter key in jQuery or disable form submission on enter key in jQuery.

Description:
  
In previous articles I explained
jQuery get current page url and title, jQuery disable future dates in datepicker, jQuery Get Cursor position, jQuery Bind double click event to button control, jQuery create rounded corners for textbox and many articles relating to JavaScript, jQuery, asp.net codesnippets. Now I will explain how to stop or disable form submission on enter key using jQuery.


If you want to stop form submission we need to write the code like this


$(function () {
$("form").submit(function (e) {
event.preventDefault();
return false;
});
})
Suppose if we need to stop form submission on enter key need to write the code like as shown below

Method 1


$(function () {
$(window).keydown(function (event) {
if (event.keyCode == 13) {
event.preventDefault();
return false;
}
});
});
Or we can use Method 2 to disable form submit on enter key
Method 2



$("form").bind("keypress", function (e) {
if (e.keyCode == 13) return false;
});

No comments:

Post a Comment

Note: only a member of this blog may post a comment.