Skip to content

Instantly share code, notes, and snippets.

@rafaell-lycan
Created January 28, 2015 22:28
Show Gist options
  • Save rafaell-lycan/f841018c5118c39d8845 to your computer and use it in GitHub Desktop.
Save rafaell-lycan/f841018c5118c39d8845 to your computer and use it in GitHub Desktop.
AngularJS - Prevent Enter Submit Directive
angular.module('app',[])
.directive('preventEnterSubmit', function () {
return function (scope, el, attrs) {
el.bind('keydown', function (event) {
if (13 == event.which) {
event.preventDefault(); // Doesn't work at all
window.stop(); // Works in all browsers but IE...
document.execCommand('Stop'); // Works in IE
return false; // Don't even know why it's here. Does nothing.
}
});
};
});
@beany-vu
Copy link

Thanks for your coding but can you give me the sample on HTML side?
Thanks a lot!!!

@josulkmx
Copy link

josulkmx commented May 24, 2018

a little bit late fot you but, here's an example
<input type="text" name="title" ng-model="form.title" prevent-enter-submit>

<form name="form" accept-charset="utf-8" prevent-enter-submit>

@crossdjinn
Copy link

thanx a lot man, saved my 📦

@nitin-rachabathuni
Copy link

awesome.

@raghvpip3
Copy link

super

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment