?
Solved

Replacing Return character in javascript

Posted on 2003-03-07
10
Medium Priority
?
854 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
  • 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
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!

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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

609 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