I'm looking for a way to sanitize input that I paste into the browser, is this possible to do with jQuery?
I've managed to come up with this so far:
$(this).live(pasteEventName, function(e) {
// this is where i would like to sanitize my input
return false;
}
Unfortunately my development has come to a screeching hold because of this "minor" issue. I would really make me a happy camper if someone could point me to the right direction.
Further:
This proved to be quite illusive. The value of the input is not updated prior to the execution of the code inside the paste event function. I tried calling other events from within the paste event function but the input value is still not updated with the pasted text inside the function of any events. That is all events apart from keyup. If you call keyup from within the paste event function you can sanitize the pasted text from within the keyup event function. like so...
There is one caveat here. In Firefox, if you reset the input text on every keyup, if the text is longer than the viewable area allowed by the input width, then resetting the value on every keyup breaks the browser functionality that auto scrolls the text to the caret position at the end of the text. Instead the text scrolls back to the beginning leaving the caret out of view.
I sort of fixed it by using the following code:
Now I just need to store the caret location and append to that position then I'm all set... I think :)
This will work for mouse paste event.
Script to remove special characters from all fields with class portlet-form-input-field: