On my asp.net form, I have a multi-line textbox and I am using the following JavaScript function on the paste event.
When I paste data, any new lines are being ignored.
I suspect there might be an issue with the JavaScript function. Even though I can see '\n' in the inner text, it is not rendering properly. For example, "hello\nworld" is displayed as "hello world".
function handlepaste(textbox, e, label, len) {
var savedcontent = textbox.innerHTML;
if (e && e.clipboardData && e.clipboardData.getData) {// Webkit - get data from clipboard, put into editdiv, cleanup, then cancel event
if (/text\/html/.test(e.clipboardData.types)) {
textbox.innerHTML = e.clipboardData.getData('text/html');
}
else if (/text\/plain/.test(e.clipboardData.types)) {
textbox.innerHTML = e.clipboardData.getData('text/plain');
}
else {
textbox.innerHTML = "";
}
waitforpastedata(textbox, savedcontent);
if (e.preventDefault) {
e.stopPropagation();
e.preventDefault();
}
return false;
}
else {// Everything else - empty editdiv and allow browser to paste content into it, then cleanup
textbox.innerHTML = "";
waitforpastedata(textbox, savedcontent, label, len);
return true;
}
}
function waitforpastedata(elem, savedcontent, label, len) {
if (elem.childNodes && elem.childNodes.length > 0) {
processpaste(elem, savedcontent, label, len);
}
else {
that = {
e: elem,
s: savedcontent
}
that.callself = function () {
waitforpastedata(that.e, that.s, label, len)
}
setTimeout(that.callself, 20);
}
}
function processpaste(textbox, savedcontent, label, len) {
pasteddata = textbox.innerHTML;
if (savedcontent.length > 0) {
textbox.innerHTML = savedcontent + pasteddata ;
}
if (pasteddata.length > len) {
textbox.value = textbox.value.substring(0, len);
document.getElementById(label).className = "red-text";
document.getElementById(label).innerHTML = 'You are trying to type too much information ';
}
else {
document.getElementById(label).className = "grey-text";
document.getElementById(label).innerHTML = textbox.value.length.toString() + ' of ' + len + ' chars';
}
}