Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
161
rated 0 times [  163] [ 2]  / answers: 1 / hits: 8336  / 11 Years ago, thu, january 2, 2014, 12:00:00

I need to prevent the single and double quotes from being pasted in a text area in javascript



HTML



<textarea rows=10 cols=10 id=txtTest></texarea>


Preventing the single and double quotes on keydown



$('#txtTest').on('keydown', function(e){
if(e.shiftKey && e.keyCode == 222 || e.keyCode == 222){
e.stopPropagation();
}
});


How to prevent the same when the text is pasted. (ctrl + v)


More From » jquery

 Answers
2

You'll need to trap more than the keydown event - Ctrl+V or right-click and paste can also insert characters (well and the older Shift+Insert). Depending on your usage, you may want to remove unwanted characters before submission. However, if you want it on the event, then trap all events and replace the text with the cleared text, depending on the event type.



$('#txtTest').val($('#txtTest').val().replace(/[']/g, ''));


That'll remove all the single and double quotes (though not smart quotes).


[#49067] Wednesday, January 1, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
billt

Total Points: 608
Total Questions: 100
Total Answers: 87

Location: Cape Verde
Member since Fri, Nov 27, 2020
4 Years ago
;