topical media & game development

talk show tell print

basic-web-html-12-ch12-eg14.htm / htm



  <?xml version="1.0" ?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  
  <head>
    <title>Removing whitespace</title>
  </head>
  
  <body>
  <h3>Removing Leading Whitespace</h3>
  <p>Enter some text with white space at the beginning of the entry, and when you move focus away from this control any leading white space will be deleted.</p>
  <form>
    <input type="text" name="txtName" size="100" 
           value="   Enter text leaving whitespace at start. Then change focus." 
           onblur="while (this.value.substring(0,1) == ' ') 
             this.value = this.value.substring(1, this.value.length);" /><br />
  </form>
  
  <h3>Removing Trailing Whitespace</h3>
  <p>Enter some text with white space at the end of the entry, and when you move focus away from this control any leading white space will be deleted.</p>
  <form>
   <input type="text" name="txtName" size="100" 
          value="Enter text leaving whitespace at end. Then change focus.   " 
          onblur="while (this.value.substring
             (this.value.length-1,this.value.length) == ' ')
             this.value = this.value.substring(0, this.value.length-1);" /><br />
  </form>
  
  <h3>Removing Whitespace Using Regular Expressions (IE4+ and Netscape 4+)</h3>
  <p>Enter some text with white space at the beginning and end of the entry, and when you move focus away from this control any leading white space will be deleted.</p>
  <form> 
    <input type="text" name="removeLeadingAndTrailingSpace" size="100"
      value="   Enter text with white space. Then change focus.   "
      onblur = "this.value = this.value.replace(/^\s+/, '').replace(/\s+/, '');"
    /><br />
  </form>
  
  </body>
  </html>


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.