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.
Hmm... I think you can use
e.clipboardData
to catch the data being pasted. If it doesn't pan out, have a look here.This is getting closer to what you might want.
Please note that when clipboardData object is not found (on browsers other then IE) you are currently getting the element's full value + the clipboard'ed value.
You can probably do some extra steps to dif the two values, before an input & after the input, if you really are only after what data was truly pasted into the element.
For cross platform compatibility, it should handle oninput and onpropertychange events:
Listen for the paste event and set a keyup event listener. On keyup, capture the value and remove the keyup event listener.
You can actually grab the value straight from the event. Its a bit obtuse how to get to it though.
Return false if you don't want it to go through.
How about comparing the original value of the field and the changed value of the field and deducting the difference as the pasted value? This catches the pasted text correctly even if there is existing text in the field.
http://jsfiddle.net/6b7sK/