0

I got to trim pasted value with JavaScript. The way I see it I got 2 options:

  • Either replace the content of clipboard with trimmed text or prevent event from continuing with event.preventDefault() and putting the trimmed value into the component myself.
  • replacing clipboard data with event.clipboardData.setData('text/plain', pastedText.trim()) didn't work.

Any ideas about those options?

ismnoiet
  • 4,129
  • 24
  • 30
  • are you getting any error? Here's the API that you can explore: https://w3c.github.io/clipboard-apis/#widl-ClipboardEvent-clipboardData – Rikin Jul 12 '16 at 23:07

2 Answers2

0

Interfering with the clipboard should generally be avoided - certainly when the user likely doesn't expect anything to happen (e.g. when pasting text). Other than that, writing to the clipboard is quite cumbersome. See for example here.

The second option is superior.

(A third option would be not to prevent the event from continuing, but to change the value in the input field after it has been pasted.)

Community
  • 1
  • 1
Bart
  • 1,600
  • 1
  • 13
  • 29
0

What is the use case for this? What are you trying to accomplish by trimming a pasted value? If possible, you shouldn't mess with the clipboard at all. Adding validation or auto input value formatting should be the way to go.

10100111001
  • 1,832
  • 1
  • 11
  • 7