Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strip Whitespace

Status
Not open for further replies.

areric

Programmer
Jul 13, 2003
47
US
Does anyone have a function that strips whitespace from a string?
 
Save this in, for example, a file called "sample.js":

Code:
function ValidateText(Argument)
  {
    Argument = Argument.replace(/ /g,"");
    return Argument;
  }

then save this as "sample.html":

Code:
<!DOCTYPE html 
     PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
     &quot;[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>[/URL]

<html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;[/URL] xml:lang=&quot;en&quot; lang=&quot;en&quot;>
  <head>
    <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;></meta>
    <title>JavaScript Sample</title>
    <script src=&quot;Sample.js&quot; type=&quot;text/javascript&quot;></script>
  </head>
  <body>
    <form name=&quot;MainForm&quot;>
      <p>Enter text below and click outside the text area to &quot;validate&quot; it.</p>
      <textarea rows=&quot;10&quot; cols=&quot;50&quot; name=&quot;Texty&quot; id=&quot;Texty&quot; onblur=&quot;document.MainForm.Texty.value=ValidateText(document.MainForm.Texty.value);&quot;></textarea>
    </form>
  </body>
</html>

Hope that helps.

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
<html>
<head>
<title>Strip White Space</title>
<script type = &quot;text/javascript&quot;>



var str = &quot;&quot;;
var whiteChars = /[ \n\r]/g;

function A(txt){
str = txt.replace(whiteChars, &quot;&quot;);
document.F.a.value = str;

}

</script>

</head>

<body bgcolor=&quot;#ffffff&quot;>

<form name = &quot;F&quot;>
<textarea name = &quot;a&quot; rows=12 cols=20 onblur = &quot;A(this.value)&quot;></textarea>

</form>
</body>

</html>
 
ps. My post above will strip linebreaks as well, if that's what you need.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top