Could someone please help. I am trying to accomplish two goals first I would like to figure out how I can intercept the text pasted into a textbox and then format the text into lines that are no longer than 58 char's long followed by a newline '\n'.

The challenges:

first in the paste: When I paste a long line of text (with no newline char's in it) the function reformatpaste(58) does take what's been pasted and reformats the text to 58 character long lines followed by the '\n' newline character and the line element.value = result; places it in the asp.net textbox. The (problem) the original text from the paste still gets copied to the textbox immediately after the formatted text. (how do I stop the original text from still being pasted in)

Second: the function formatnote() works great when the user types in the textbox. The text is reformatted at the 58 character point where a newline character is added to the line. But if you paste text in and then try editing in the middle of the text, the new characters just spool over and lines go beyond the 58 character point.

Would anyone have any ideas to help:

Thanks



Here is what I have so far.

<asp:TextBox ID="txtNote" runat="server" TextMode="MultiLine" Width="79%" Height="200px"
Style="margin-left: 40px;" OnKeypress="formatnote(58)" onpaste="reformatpaste(58)" BackColor="#FFF6E5">
</asp:TextBox>

function formatnote(max_chars) {
var element = document.getElementById(txtnote);
var str2;
var idx = 0;

if (event.keyCode != 13)
{
if ((element.value.length + 1) > max_chars)
{
idx = element.value.lastIndexOf("\n");
if (idx > 0)
{
str2 = element.value.substr(idx + 2, max_chars);
if ((str2.length + 2) > max_chars)
{
element.value += "\n";
}
}
else {
element.value += "\n";
}
}
}
}

function reformatpaste(max_chars) {
var element = document.getElementById(txtnote);
var data = clipboardData.getData('Text');
var result = '';

var str = data.replace(/(\r\n|\n|\r)/gm, "");

while (str.length > 0) {
if (str.length > max_chars) {
result += str.substring(0, 58) + '\n';
}
else {
result += str;
}
str = str.substring(58);
}

element.value = result;
event.returnValue = false;
}