How to disable Submit from a form by "Enter" - c# MVC

Asked

Viewed 1,677 times

0

I am developing a web project in c#, and for Forms I am using the Html.BeginForm. How to disable Submit by pressing the key Enter? Can do without javascript?

3 answers

4


Without javascript I find it hard to get, you’ll need a script Client Side to check the click. With jQuery can do the following:

$(document).ready(function() {
    $('form#exemplo').bind("keypress", function(e) {
        if ((e.keyCode == 10)||(e.keyCode == 13)) {
            e.preventDefault();
        }
    });
});

or

$(document).ready(function() {
    $('form#exemplo').keypress(function(e) {
        if ((e.keyCode == 10)||(e.keyCode == 13)) {
            e.preventDefault();
        }
    });
});

2

If you have a Submit button in this form it must be marked as "default" and the keystroke action Enter executes the component marked as such.

2

You can also turn Submit into a button.

<input type='button'>

And submit the form via JS.

$('#form_id').submit(); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.