最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to detect paste in text input area if user paste a whole text or string in it? - Stack Overflow

programmeradmin2浏览0评论

Note : Do not mark this as a duplicate question, sure this is not. But it is related to certain question out there.

There are click event or touch event to detect paste in the text field. But now there is a new trick to paste content in text field using mobile "swift keyboard" android keyboard. Which enables clipboard option to paste the predefined text into any input field. I tried almost every online answers and this clipboard works without detecting any event in the browser.

So help me out to solve this issue, i want to detect this certain clipboard event in the text field and block users from using it.

<textarea name="msg" placeholder="Type message here...." class="materialize-textarea" onCopy="return false" onDrag="return false" onDrop="return false" onPaste="return false" oncontextmenu="return false" onkeydown="if ((arguments[0] || window.event).ctrlKey) return false"></textarea>

Sure this blocks normal paste in desktop or mobile devices. But, it can't detect certain clipboard content paste.

I need a real solution to fix this issue.

Note : Do not mark this as a duplicate question, sure this is not. But it is related to certain question out there.

There are click event or touch event to detect paste in the text field. But now there is a new trick to paste content in text field using mobile "swift keyboard" android keyboard. Which enables clipboard option to paste the predefined text into any input field. I tried almost every online answers and this clipboard works without detecting any event in the browser.

So help me out to solve this issue, i want to detect this certain clipboard event in the text field and block users from using it.

<textarea name="msg" placeholder="Type message here...." class="materialize-textarea" onCopy="return false" onDrag="return false" onDrop="return false" onPaste="return false" oncontextmenu="return false" onkeydown="if ((arguments[0] || window.event).ctrlKey) return false"></textarea>

Sure this blocks normal paste in desktop or mobile devices. But, it can't detect certain clipboard content paste.

I need a real solution to fix this issue.

Share Improve this question asked Jan 26, 2019 at 11:03 Joseph MillerJoseph Miller 1993 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Instead of detecting whether a paste event has occurred, you can listen for an input event on the textarea. Then by keeping track of the previous length of the string in the textbox you can see if the input has changed by more than one character.

See working example below:

window.onload = function() {
  const textarea = document.querySelector('.materialize-textarea');
  let prev_val = textarea.value;
  let prev = prev_val.length;

  textarea.addEventListener('input', function() {
    let new_len = this.value.length;
    let dif = new_len - prev;
    if (dif > 1) {
      alert("paste detected!");
      textarea.value = prev_val;
    } else {
      prev = new_len;
    }
    prev_val = this.value;
  });
}
<textarea name="msg" placeholder="Type message here...." class="materialize-textarea"></textarea>

Although, this solution does have its limitations.

发布评论

评论列表(0)

  1. 暂无评论