In text fields of my JSP, I wish to know whether user is typing in the data or just pasting. How can I identify this using javascript ?
EDIT: As per Andy's answer I know how I can go about it, but still curios how those guys wrote onpaste event.
Safari, Chrome, Firefox and Internet Explorer all support the onpaste
event (not sure about Opera). Latch onto the onpaste
event and you will be able to catch whenever something is pasted.
<input type="text" id="myinput" onpaste="handlePaste(event);">
or JavaScript-DOM:
var myInput = document.getElementById("myInput");
if ("onpaste" in myInput) // onpaste event is supported
{
myInput.onpaste = function (e)
{
var event = e || window.event;
alert("User pasted");
}
}
// Check for mutation event support instead
else if(document.implementation.hasFeature('MutationEvents','2.0'))
{
/* You could handle the DOMAttrModified event here, checking
new value length vs old value length but it wouldn't be 100% reliable */
}
From what I've read, Opera does not support the onpaste
event. You could use the DOMAtrrModified
event, but this would fire even when scripts change the value of the input box so you have to be careful with it. Unfortunately, I'm not familiar with mutation events so I wouldn't like to mess this answer up by writing an example that I wouldn't be confident of.
Count the key presses and make sure it matches whats in the text box a paste will not have complete number of characters as is in the text box.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With