How to get all of the pasted string, in input which has a maxLength attribute?

Abo*_*kar 10 html javascript string clipboard paste

I need to get all of the pasted string in input which has a maxLength attribute.

But in 'onpaste' event there is no property to get all of the pasted string.

For example, check below snippet with this string:

"AAAAA-BBBBB-BBBBB-BBBBB-BBBBB"

The output is : "AAAAA"

But I need all of the string.

const onPasteFn = (e) => {
  setTimeout(() => document.getElementById("demo").innerHTML = e.target.value, 0)
}
Run Code Online (Sandbox Code Playgroud)
<input type="text" maxLength="5" onpaste="onPasteFn(event)" />

<p id="demo"></p>
Run Code Online (Sandbox Code Playgroud)

Nic*_*ons 8

考虑使用clipboardDatafrom 事件,您可以使用它getData()来抓取从剪贴板粘贴的文本,如下所示:

const onPasteFn = (e) => {
  document.getElementById("demo").innerHTML = (e.clipboardData || window.clipboardData).getData('text');
}
Run Code Online (Sandbox Code Playgroud)
<input type="text" maxLength="5" onpaste="onPasteFn(event)" />

<p id="demo"></p>
Run Code Online (Sandbox Code Playgroud)

见例如这里从文档。请注意,后备|| window.clipboardData用于 IE 支持。