Showing posts with label disable submit when hitting ENTER. Show all posts
Showing posts with label disable submit when hitting ENTER. Show all posts

Friday, January 27, 2017

asp.net mvc disable submit when ENTER key is pressed

When you create a view with a submit button, the default behavior is submit will be triggered on KEY PRESS events.

If your view has a BeginForm, following code will stop submitting on KEY PRESS.

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { onkeydown = "return event.keyCode!=13" }))
{
    //code
}


Alternatively you can use jQuery to resolve the same issue as follows

<script>

 $(function () {
         $(document).on("keydown", "input", function(e) {
             if (e.which==13) e.preventDefault();
        });
  });

</script>