Click to See Complete Forum and Search --> : Formatting textbox or Word wrapping


pn1978
12-08-2006, 08:51 AM
I found on the web JS code to insert a break inside a textbox at the specified character count. The script does what I need except on major thing, it does not check if the specified character is a space. What I need the script to do is to check if the specified character is a space, if it is it should proceed, if it is not, I want it to look backwards for the first space and insert a break there. I only have some html and ColdFusion background. I tried couple of loops and if statements but it does not work. Any suggestions or pointers? Any help is appreciated.
Thanks.
example of the code

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function showLines(max, text) {
max--;
text = "" + text;
var space = " ";
var temp = "";
var chcount = 0;
for (var i = 0; i < text.length; i++) // for each character ...
{
var ch = text.substring(i, i+1); // first character
var ch2 = text.substring(i+1, i+2); // next character
if (ch == '\n') // if character is a hard return
{
temp += ch;
chcount = 1;
}
else
{
if (chcount == max) // line has max chacters on this line
{
if (ch == " ")
{
temp += '\n' + ch; // go to next line
chcount = 1; // reset chcount
}
else
{
var lastSpace = temp.lastIndexOf('w', 1);
alert(lastSpace);
alert(temp)
//return();
}
}
else // Not a newline or max characters ...
{
temp += ch;
chcount++; // so add 1 to chcount
}
}
}
return (temp); // sends value of temp back
}
// End -->
</script>

<textarea name="test" cols="55" rows="5" onChange="javascript:this.form.test.value = showLines(55, this.form.test.value); this.value=this.value.toUpperCase();"></textarea>

mjdamato
12-08-2006, 12:54 PM
This should work a little better (tested in IE only). But, assuming you are posting this data to a page where it will be processed, that might be a better place to do this. I say that because if a user inserts a line break, then you shouldn't remove that line break. But, when the script has one once and entered a line break, if the user modifies the text, they will have to remove unwanted line breaks.

<html><head>

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin

function showLines(maxLength, field) {
var text = field.value;
var newText = "";
var tmpIdx = 1;

textAry1 = text.split(String.fromCharCode(13));

for (var i=0; i<textAry1.length; i++) {

textAry = textAry1[i].split(String.fromCharCode(10));

for (var j=0; j<textAry.length; j++) {

rowText = textAry[j];

while (rowText.length && (rowText.length > maxLength)) {
tmpIdx = rowText.lastIndexOf(" ", maxLength);
if (tmpIdx == -1) {
tmpIdx = rowText.lastIndexOf(" ");
}
if (tmpIdx == -1) {
tmpIdx = rowText.length-1;
}

newText += rowText.substr(0, tmpIdx) + String.fromCharCode(13);
rowText = rowText.substr(tmpIdx+1)
}
newText += rowText;
if (j<textAry.length-1) { newText += String.fromCharCode(13); }
}
}
return newText;
}



// End -->
</script>

</head>

<body>

<form name=theform>
<textarea name="test" cols="75" rows="5"
onChange="javascript:this.value = showLines(55, this);"></textarea>
</form>

</body></html>

pn1978
12-21-2006, 10:40 PM
Thanks for your help. Most likely it would take me couple of weeks to figure this out. The script works pretty well. I can follow some of the code but not everything. Is it too much if I ask for some comments so I can understand it better?
Thanks again.