/    Sign up×
Community /Pin to ProfileBookmark

Removing a newline from text field

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.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@oleragJul 15.2003 — 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>
Copy linkTweet thisAlerts:
@CharlesJul 15.2003 — [font=georgia]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. [/font]

[font=monospace]<!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(/[nrl]+/g, '~')"></textarea>

</div>

</form>[/font]
Copy linkTweet thisAlerts:
@oleragJul 15.2003 — 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("rn","g");

else

if (browserName = "Netscape")

var re = new RegExp("n","g");

var newVal = val.replace(re, "~");
document.forms[0].fieldArea1.value = newVal;

}
Copy linkTweet thisAlerts:
@CharlesJul 15.2003 — [font=georgia]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.[/font]
Copy linkTweet thisAlerts:
@oleragJul 15.2003 — 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"??
Copy linkTweet thisAlerts:
@CharlesJul 15.2003 — [font=georgia]Thanks for catching that. It seems that MSIE is also ignorant about that escape character. Use instead:[/font]

[font=monospace]<!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(/[nr]+/g, '~')"></textarea>

</div>

</form>[/font]
Copy linkTweet thisAlerts:
@SpudlabsMay 15.2014 — @Charles

Thank you very much. Have been searching for this for hours. Simple, elegant and easily implemented.

Cheers
×

Success!

Help @mh53j_fe spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...