Very similar to this question, but I don't want to hijack that thread with my specifics.
How to disable special characters from paste in a textbox
I have a form to display a jquery grid and a search textbox above it. I want to prevent our customers from pasting text that contains special characters (like a tab control) because it causes the jquery to hang.
I modified my file to include what looks like the best solution for my scenario:
$(document).ready(function() {
$('input').bind('paste', function() {
setTimeout(function() {
var obj = ('input').val();
var text = obj.replace(/\W+/g, ' ');
$('input').val(text);
});
});
});
The function would go in the $(document).ready
section, right?
Does all does data.replace(/[^\w\s]/gi, '')
replace? I want to catch \t for sure - possibly others.
My jQuery-fu and regex-fu are not great, so there could be issues with those.
With the script above added, I am still able to paste a simple string like a b
into the textbox and cause jquery to hang.
<body>
<div>The text below has a tab between the <em>a</em> and the <em>b</em>:</div>
<pre>a b</pre>
<div>Modify the regular expression in the javascript to replace the tab with a single space whenever you paste it into the text box below.</div>
<div><input type="text"></div>
<div><a href="#">x</a></div>
</body>