How to disable the submit of a form by "Enter" - C # MVC

0

I'm developing a web project in C #, and for forms I'm using Html.BeginForm . How do I disable submit by pressing the Enter key? Can you do without javascript?

    
asked by anonymous 16.11.2017 / 11:26

3 answers

3

Without javascript I find it difficult to achieve, you will need a script Client Side to check the click. With jQuery you 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();
        }
    });
});
    
16.11.2017 / 11:34
2

If you have a Submit button on your form it should be marked as "default" and the action of typing Enter will execute the marked component as such.

    
16.11.2017 / 11:51
1

You can also turn the submit into button.

<input type='button'>

And submit the form via JS.

$('#form_id').submit(); 
    
17.11.2017 / 16:48