Remove whitespace and replace all chr(13) and chr(10)

Posted on 2005-05-10
Last Modified: 2011-04-14
Hi all....

I need a script that will, onKeyUp, remove/prevent carriage returns 'chr(13)', line feeds 'chr(10), and multiple spaces in a text area. When a user is typing into the text area and hits Return, it prevents it. If the user presses space multiple times, it only allows a single space.

I have the following from a few scripts I found on EE (I believe part is ZVonko's script) but I don't understand RegExp well enough to edit it to do what I need it to do. It seems to handle the carriage returns well but prevents any spaces from being entered.

function removeChar(input) {
var output = "";
for (var i = 0; i < input.length; i++) {
if ((input.charCodeAt(i) == 13) && (input.charCodeAt(i + 1) == 10)) {
output += " ";
} else {
output += input.charAt(i);
return output;

String.prototype.fullTrim = function()
   return this.replace(/\s+/g," ").replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,"$1");

<textarea name='txtUserData' rows='8' cols='50' onKeyUp="removeChar(this.form.txtUserData.value);this.value=this.value.fullTrim()"></textarea>

Thanks in advance,
Question by:Chris24
    LVL 25

    Accepted Solution

    hi Chris24, try this:
    <textarea name='txtUserData' rows='8' cols='50' onKeyUp="this.value=this.value.replace(/ {2,}/g,' ').replace(/[\n\r]*/g,'');"></textarea>

    Author Comment

    Perfect!! Thank you.

    I just created a function from it.

    function funcFormat(theValue) {
          theValue.value=theValue.value.replace(/ {2,}/g,' ').replace(/[\n\r]*/g,'');

    And changed the onKeyUp event to: onKeyUp="return funcFormat(this)"

    I greatly appreciate it!

    Thank you
    LVL 25

    Expert Comment

    you are welcome!

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    In Part 1 ( 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'…
    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…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now