Click to See Complete Forum and Search --> : Removing a newline from text field
mh53j_fe
07-15-2003, 07:17 AM
I want to remove the newline from text paragraphs. I have tried this:
function isNewLine( field )
{
var l_field_value = field.value;
var l_field_str = l_field_value.toString();
var l_oneChar;
for( var i = 0; i < l_field_str.length; i++ )
{
l_oneChar=l_field_str.charAt(i);
if( l_oneChar=="\n" )
{
alert("#2 l_oneChar == " + l_oneChar);
field.value.replace("\n", "~");
//return false;
}
}
return true;
This does not work. I would appreciate any help possible.
olerag
07-15-2003, 08:26 AM
This function, using the "replace()" function, should do the
trick, however, when I attempted in IE6 the "line feed" in
the "text area" is replaced with the "tilde" but the new line
affect continues. Strange - perhaps someone else might
know why this happens for this browser's "text area" object.
It works fine in NN7+.
<SCRIPT Language=JavaScript>
function changeText() {
var val = document.forms[0].fieldArea1.value;
var re = new RegExp("\n","g");
var newVal = val.replace(re, "~");
document.forms[0].fieldArea1.value = newVal;
}
</SCRIPT>
Charles
07-15-2003, 08:39 AM
1) The "language" attribute was depricated back in 1997;
2) There's no need to create a variable that you are only going to use once;
3) You can shorten that a lot by using the "onchange" handler;
4) Yet another example of the why MSIE is a piece of dung, you need to adjust the regular expression a bit to make it work in MSIE.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<form action="">
<div>
<textarea onchange="this.value = this.value.replace(/[\n\r\l]+/g, '~')"></textarea>
</div>
</form>
olerag
07-15-2003, 08:53 AM
Charles is much faster than me. I found this by messing
with the various escape strings. IE (I guess) uses both the
carridge return and line feed for new lines in text areas. Netscape only uses the line feed. For other browser's, you can adjust as needed.
Here's my adjusted function but Charles' approach is
certainly "cleaner".
<SCRIPT Language=JavaScript>
function changeText() {
var browserName = navigator.appName;
var val = document.forms[0].fieldArea1.value;
if (browserName == "Microsoft Internet Explorer")
var re = new RegExp("\r\n","g");
else
if (browserName = "Netscape")
var re = new RegExp("\n","g");
var newVal = val.replace(re, "~");
document.forms[0].fieldArea1.value = newVal;
}
Charles
07-15-2003, 09:02 AM
The escape character '\l' represents a line feed, '\r' a carriage return but '\n' is supposed to represent whatever the system uses to end a line. MSIE is wrong to not recognize it. My regular expression creates a class of new lines sequences, carriage returns and line feeds and replaces one or more of them in a row with a single '~'. That should cover all of the bases.
And your new try commits most of the sins of your former and adds to the list that it will not work on any browser other than MSIE or Netscape.
olerag
07-15-2003, 09:49 AM
I agree with you Charles. Your pseudo-code example on
the event is clean and removes browser typing.
One question. The "\l" (small L) in your code replaces all "l"s with a tilde; i.e., "Hello" becomes "He~o".
When I remove the "\l" from your code, all works well. Is this correct or is the character a numeric "one"??
Charles
07-15-2003, 09:55 AM
Thanks for catching that. It seems that MSIE is also ignorant about that escape character. Use instead:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<form action="">
<div>
<textarea onchange="this.value = this.value.replace(/[\n\r]+/g, '~')"></textarea>
</div>
</form>