/    Sign up×
Community /Pin to ProfileBookmark

Removing trailing and leading newlines

Hi,
I would like to eliminate all trailing and leading newlines that would appear in a textarea on my web page. However, I would like not like to have all the text transformed into a single paragraph.

Can anyone help me with this?

I am using ASP.

Thanks in advance,
Tarun

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@PittimannDec 11.2003 — Hi!

I wanted the same as you quite a long time ago; as I didn't know, if there is a predifined stuff to do that in Javascript, I tried to create a makeshift in order to get rid of linebreaks and spaces at the beginning end the end of the user's input.

For me it worked fine and until now, I don't know, if there is a "professional" solution for that in js...

Try this:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function check(){
temp = document.forms[0].textInput.value;
temp = temp.replace(/ /g, "ÿ");//replace spaces with ÿ
temp = temp.replace(/s/g, "µ");//replace all remaining white spaces with µ
out="µ";
out2="ÿ";
while (temp.indexOf(out)==0||temp.indexOf(out2)==0) {// replace leading µ's and ÿ's
temp = (temp.substring(1, temp.length));
}
while (temp.substring(temp.length-1,temp.length)==out||temp.substring(temp.length-1,temp.length)==out2) { //replace trailing µ's and ÿ's
temp = (temp.substring(0, temp.length-1));
}
temp = temp.replace(/µµ/g, "n");//change µ's in text back to linebreaks
temp = temp.replace(/ÿ/g, " ");//change ÿ's in text back to spaces
document.forms[0].textInput.value=temp;
}
//-->
</script>
</head>
<body>
<form>
<textarea name="textInput" rows=8 cols=45></textarea><br>
<input type=button value=click onclick="check()">
</body>
</html>
[/code]

Cheers - Pit

BTW - I hope, you're not going to hang me, if a user really enters "ÿ" or "µ" :p (if you're dealing with maths or engineering, you could at least use something other than "µ" for that part of the job)...
Copy linkTweet thisAlerts:
@tarunauthorDec 11.2003 — Hi Pit,

Thank you for the speedy reply. I wasn't looking for a professional solution, just some code that would work. ? Unfortunately I tried the code and it's not removing the leading and trailing new lines.? .

I'm completely new to javascript so pardon my ignorance if what I say seems quite elementary.

I am planning to retrieve info from the input text area and display it on another page without the leading and trailing newlines. With your code, I am neither getting an error nor the correct output.?

Is there any way you can help?

Regards,

Tarun
Copy linkTweet thisAlerts:
@PittimannDec 11.2003 — Hi!

Right now, I tested that at home on a PC Win98 with IE6 Opera 6 and NS4.7. The site, for which it was originally used was visited by people, whose machines ran with different OS's and browsers.

So far I haven't heard, that the stuff doesn't work - I don't know, what might be wrong. Did you use the code like I posted it, or did you put it into your own file?

Cheers - Pit ?
Copy linkTweet thisAlerts:
@PittimannDec 11.2003 — Hi!

A tiny little thing came to my mind:

if you have more than one form in your html and the form, the code I posted is dealing with, is not the first one in your page, the stuff will fail.

If so, either make the form in question the first one of your page or replace "forms[0]" with "forms[x]", x being the number of the form minus one (second form => "forms[1]" etc.).

Maybe that was the reason...

Cheers - Pit
Copy linkTweet thisAlerts:
@ray326Dec 11.2003 — Lots of assuming:

<form name="f">

<textarea name="ta">

</textarea>

...

</form>

Then

document.f.ta.value = document.f.ta.value.replace(/rn|n|r/g, " ")

will do the job.
Copy linkTweet thisAlerts:
@PittimannDec 11.2003 — Hi!

To ray326:

The job it does is alright (replacing linebreaks with spaces). What tarun wanted is:
I would like to eliminate all trailing and leading newlines that would appear in a textarea on my web page. However, I would like not like to have all the text transformed into a single paragraph.[/QUOTE]
Due to that some more action is required...

Cheers - Pit
×

Success!

Help @tarun 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.18,
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,
)...