title: How to trim a space at the start and end of textbox?
body: <p>I want to trim any spaces at the start of the text box and trim any spaces at the end of the textbox. So I have found this code on a website which is suppose to remove spaces at the start, end and multiple spaces in between:</p>
  
  <pre><code>function trim(s) {
      s = s.replace(/(^\s*)|(\s*$)/gi,"");
      s = s.replace(/[ ]{2,}/gi," ");
      s = s.replace(/\n /,"\n");
      return s;
  }
  </code></pre>
  
  <p>My problem is though that first of all which one of the 3 lines of code is the one where it trims spaces in the middle because I don't need that one. But the main question is how do I get the textbox to access this function?</p>
  
  <p>I tried using onkeypress but this hasn't worked, below is what I have tried:</p>
  
  <pre><code>&lt;p&gt;Search: &lt;input type="text" name="questioncontent" onkeypress="return trim(s)" /&gt;&lt;/p&gt;
  </code></pre>
  
  <p>So what I want is that for example if this phrase is entered in textbox ' My Name is Pete '. Then it should remove the spaces at the start and end so it reads 'My Name is Pete'. But how do I get this to work? </p>
  
  <p>UPDATE:</p>
  
  <p>Found out that trim() is jQuery, so does anyone a javascript equivalent for this which can be hand coded to remove spaces at start and end of textbox?</p>