How to block carriage input in textarea?

How to block carriage input, new line, single quotes and double quotes in a text box using asp.net mvc during a keypress event?

+7
source share
2 answers

You can use jquery and subscribe to the .keypress() event textarea:

 $('textarea').keypress(function(event) { // Check the keyCode and if the user pressed Enter (code = 13) // disable it if (event.keyCode == 13) { event.preventDefault(); } }); 
+27
source

To be convenient with the modification of the text caused by the user dragging another text / elements inside the text field or the insertion text inside it, you need to listen to change .

In addition, I suggest using the .on() method, available with jQuery 1.7, and defining the input key pressed by the user through the event.which property of the event object to have solid cross-browser behavior for your application:

 var $textarea = $('#comment'); // events to bind: $textarea.on('keydown keyup change focus blur', function(e) { if (e.type === 'change') { // this event is triggered when the text is changed through drag and drop too, // or by pasting something inside the textarea; // remove carriage returns (\r) and newlines (\n): $textarea.val($textarea.val().replace(/\r?\n/g, '')); } if (e.which === 13) { // the enter key has been pressed, avoid producing a carriage return from it: e.preventDefault(); } }); 
+2
source

All Articles