Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable enter key on specific textboxes

Tags:

jquery

People also ask

How do you disable the Enter key of an input textbox?

Disabling enter key for the formaddEventListener('keypress', function (e) { if (e. keyCode === 13 || e. which === 13) { e. preventDefault(); return false; } });

What is e keyCode === 13?

key 13 keycode is for ENTER key.

How do I turn off keypress events?

Alternatively, you could attach another event handler to the input field, and in this handler stop the propagation of the event: jQuery('#input-field-id'). bind('keypress', function(e) { e. stopPropagation(); });


This works for me.

$('textarea').keypress(function(event) {
    if (event.keyCode == 13) {
        event.preventDefault();
    }
});

jsFiddle.

Your second piece looks like it is designed to capture people pasting in multiple lines. In that case, you should bind it to keyup paste.


If you want to prevent Enter key for specific textbox then use inline js.

<input type="text" onkeydown="return (event.keyCode!=13);"/>

It stops user to press Enter in Textbox & here I return false which prevent form to submit.

$(document).ready(function()
    {
        // Stop user to press enter in textbox
        $("input:text").keypress(function(event) {
            if (event.keyCode == 13) {
                event.preventDefault();
                return false;
            }
        });
});

Works for me:

$('#comment').keypress(function(event) {
    if (event.which == 13) {
        event.preventDefault();
    }
});

http://jsfiddle.net/esEUm/

UPDATE

In case you're trying to prevent the submitting of the parent form rather than a line break (which only makes sense, if you're using a textarea, which you apparently aren't doing?) you might want to check this question: Disable the enter key on a jquery-powered form


I found this combine solution at http://www.askmkhize.me/how-can-i-disable-the-enter-key-on-my-textarea.html

$('textarea').keypress(function(event) {

if ((event.keyCode || event.which) == 13) {

    event.preventDefault();
    return false;

  }

});

$('textarea').keyup(function() {

    var keyed = $(this).val().replace(/\n/g, '<br/>');
    $(this).html(keyed);


});

If you want to disable for all kind of search textbox. Give all them a class and use that class like 'search_box' following.

//prevent submission of forms when pressing Enter key in a text input
$(document).on('keypress', '.inner_search_box', function (e) {
    if (e.which == 13) e.preventDefault();
});

cheers! :)