Clean way to prevent enter button submitting a form Clean way to prevent enter button submitting a form asp.net asp.net

Clean way to prevent enter button submitting a form


Set the UseSubmitBehavior property on the submit button to FALSE. (found the solution here)


The following code solved my issue:

http://www.itorian.com/2012/07/stop-from-submission-posting-on-enter.html

<script type="text/javascript">    //Stop Form Submission of Enter Key Press    function stopRKey(evt) {        var evt = (evt) ? evt : ((event) ? event : null);        var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);        if ((evt.keyCode == 13) && (node.type == "text")) { return false; }    }    document.onkeypress = stopRKey;</script>

Put this in the master page or just in the pages that you want.


Use Submit behavior didn't work for me but this

 $(function () {        $(window).keydown(function (e) {            if (e.keyCode == 13) {                e.preventDefault();                return false;            }        });    })

worked for me