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.
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 theonpaste
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:
or JavaScript-DOM:
From what I've read, Opera does not support the
onpaste
event. You could use theDOMAtrrModified
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.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).
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.
I know for textarea you can capture on paste event using the
onPaste
event.HTML:
In JS:
As for typing, there's
onKeyDown
,onKeyUp
,onKeyPress
events.Hope this helps.
Possible SO-related question IE onPaste event using javascript not HTML