views:

172

answers:

4

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.

+1  A: 

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.

Paul Whelan
That's smart but not water proof. What if I pasted after typing, maybe after using backspace to start over?
Sandor Drieënhuizen
Check the total number of characters in the text box. A paste will increase this by larger than what your on key event detected. The delete key has a code look for it and decrement the count :)
Paul Whelan
You can paste from the context menu without using any keys so your handling event couldn't capture this.
Andy E
Yes you can but the characters still appear in the text box so you know that the count of characters in the text box is more than what was typed so you can detect a paste took place.
Paul Whelan
I see above there's a onpaste event that solves it. But I guess you're right. I could go on about pasting a single character over another but then I would be nit picking :)
Sandor Drieënhuizen
Hey Sandor I was actually thinking that myself :)
Paul Whelan
A: 

You will never know for sure. Even when intercepting key input, the use may have used the context menu to paste using the mouse. Accessing the clipboard (to compare the input with the clipboard contents) will not work the way you want because it is a strict user-only operation. You are not able to access is programmatically without the explicit consent of the user (the browser will show a confirmation message).

Sandor Drieënhuizen
+8  A: 

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.


Writing this is simple enough. Add the event handler to your input using html:

<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.

Andy E
Never knew this existed thats why I love SO.
Paul Whelan
Life is good here
Ravi Gupta
Another anonymous downvoter too rude to leave a comment?
Andy E
The paste event only works in Firefox from version 3.0.
Tim Down
@Tim Down: True, but I didn't think it worth mentioning since very few people use Firefox 2.0.
Andy E
I think it's always worth mentioning these things. I'd stop short of mentioning whether something works in IE 4, possibly even IE 5, but Firefox 2.0 isn't that old.
Tim Down
A: 

I know for textarea you can capture on paste event using the onPaste event.

HTML:

<textarea id="textEditor" />

In JS:

var editor = document.getElementById("textEditor");

if (isIE /* determine this yourself */) {
   editor.onPaste = function() {

   }
} else {
   //Not IE
   editor.onpaste = function() {

   }
}

//The capitalisation of the onpaste (non-IE) and onPaste (IE) makes a difference.

As for typing, there's onKeyDown, onKeyUp, onKeyPress events.

Hope this helps.

Possible SO-related question http://stackoverflow.com/questions/546905/ie-onpaste-event-using-javascript-not-html

The Elite Gentleman