I’d like to have an asp.net textbox that people can paste content into and it works like notepad, i.e. no formatting or special characters will get entered. I take text and pass it to a web service which manipulates it and converts it into a tab delimited file. The problem I’ve experienced is sometimes people copy from MS Word and paste that content in and somehow even the tab characters etc. get passed to the web service. I run routines now to strip that information out but it would be so much easier if the textbox on the web page didn’t capture anything but the text itself, i.e. visible characters (numbers, letters, punctuation).
Anyone have a suggestion to have a textbox that doesn’t capture formatting and non-visual characters?
Thank you.
Short answer:
You can’t.
Long answer:
There are no javascript events for copy/paste that works for all browsers. There are a limited support, you can find a table here: http://www.quirksmode.org/dom/events/cutcopypaste.html
As for specifying what to receive in an input element, you can really specify that either.
As I see it you got three options:
a) Do as the others say and handle the change event on the input element. Modify the value on change to strip and unwanted characters. (
<input onchange="return myFilterFunc(this)">)b) Hook the lost focus event strip any unwanted characters exist in the input. (
<input onblur="return myFilterFunc(this);">)c) Filter on form submit (
<form onsubmit="return myFilterFunc(this)">).You can find all javascript events here: http://www.comptechdoc.org/independent/web/cgi/javamanual/javaevents.html