?
Solved

Replacing Return character in javascript

Posted on 2003-03-07
10
Medium Priority
?
851 Views
Last Modified: 2008-02-01
I have a form that i am trying to keep return characters out of.  How can I replace return characters with a whitespace?

Thanks in advance for answering this bonehead kwestion.

the Big. the Bad. the JH.
0
Comment
Question by:Agent47
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 3

Expert Comment

by:bjrcreations
ID: 8089735
Do you mean like keeping a carriage return "\r" or new line "\n" character?
0
 

Author Comment

by:Agent47
ID: 8089752
I don't know the code for the characters, but I want to keep line breaks out of the text boxes.
0
 

Author Comment

by:Agent47
ID: 8089777
I don't know the code for the characters, but I want to keep line breaks out of the text boxes.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:Agent47
ID: 8089792
Basically, how do you keep line breaks out of a text field?
0
 
LVL 3

Expert Comment

by:bjrcreations
ID: 8089813
<SCRIPT>
   function test() {
       var re = new RegExp(/\n/g);
       var str = document.myForm.myText.value;
       if (re.test(str)) {
           alert("No line breaks allowed.");

           // if you want to delete line breaks
           document.myForm.myText.value = str.replace(/\r\n/g," ");

           return false;
       } else {
           return true;
       }
   }
</SCRIPT>

<FORM name="myForm" onSubmit="return test()">
   <TEXTAREA name="myText" cols=20 rows=20></TEXTAREA>
   <INPUT type=submit value="test">
</FORM>
0
 
LVL 3

Expert Comment

by:bjrcreations
ID: 8089815
try the code above
0
 

Author Comment

by:Agent47
ID: 8089878
I appreciate the help, but that actually janked up my page.  I am doing the charreplace function stuff in an onBlur event, and (inept as I am) I can't seem to get this code going with that event.
0
 

Accepted Solution

by:
jestersded earned 80 total points
ID: 8090872
try this...

<textarea onblur="this.value=this.value.replace( /\s/g, ' ' )"></textarea>

you might notice that when you use this, you get two spaces between the words.  this is because the "return" key is actually inserting two characters \r\n (cariage return (go to the beginning of the line), line feed (go down one line)), just like old typewriters.  If you don't like the two spaces, try this:

<textarea onblur="this.value=this.value.replace( /\s/g, ' ' ).replace(/(\s)\s/g, '$1' )"></textarea>

Good Luck. Patrick.
0
 

Author Comment

by:Agent47
ID: 8091324
Jester...

You are a star.
0
 

Author Comment

by:Agent47
ID: 8091333
You are Star-Quality!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question