title: Conditionally remove special characters from a string while being typed
body: <p>I am attempting to attach a function to a form field so that - preferably as the user is typing into the field - special characters that I specify will be stripped out of the typed text. There are some conditions, however:</p>
  
  <ul>
  <li>Opening &amp; closing parentheses are allowed</li>
  <li>Spaces are allowed, unless it's a trailing space at the end or two spaces in a row</li>
  </ul>
  
  <p>I've found several examples of how to replace special or non-word characters from the string after the fact, but none so far on how to just strip out the characters I don't want, or how to remove spaces ONLY when it's a trailing space.</p>
  
  <p>Note that I'm not married to JavaScript vs. jQuery (or vice versa), but it needs to be one or the other. I'm also not married to doing it on the fly, but that's the preferable option - some other functions already occur on the next field, so I'd rather have the proper value before the user tabs out of this particular text box.</p>
  
  <p>I've attempted to use multiple replaces like the example below but it doesn't seem to like replacing a character with nothing (""). I'm also not sure how to write the condition for the trailing space.</p>
  
  <pre><code>var s1 = "I live on Hopper St.";
  var s2 = s1.replace(/\./g, "");
  </code></pre>
  
  <p>This is primarily intended to auto-correct the three main problems we see:</p>
  
  <ul>
  <li>User enters a road name or abbreviation WITH period (St.) </li>
  <li>User accidentally hits space twice, resulting in a double-space</li>
  <li>User accidentally hits space at end, resulting in a trailing space</li>
  </ul>
  
  <p>These values are used in a lengthy business process, passing the value to other fields, creating folders, etc. so things like periods, double-spaces, and trailing spaces all gum up the works later on. I'm trying to head those issues off at the pass by killing them in the first field.</p>