title: Validate html text input as it's typed
body: <p>What's the best way of validating an HTML text input as it's typed? All ways I know of doing it has some drawbacks:</p>
  
  <ol>
  <li><p>Using <code>$.keypress</code> you only have access to the input's old value, not the new one. Also, some events (like cutting/pasting using the mouse) won't be detected.</p></li>
  <li><p>Using <code>$.change</code> only works when the input loses focus.</p></li>
  <li><p><a href="https://stackoverflow.com/q/1546348/520779">This question</a> proposes a solution, where you use polling to watch for property changes. In principle, it's possible to validate the new value in the callback, then revert to the old one if invalid. However, besides requiring polling, I'm not sure it's free from race conditions.</p></li>
  <li><p><a href="https://www.darcyclarke.me/articles/development/detect-attribute-changes-with-jquery/" rel="nofollow noreferrer">This article</a> suggests using browser specific features when present, falling back to polling if none is available. Looks like the best approach so far.</p></li>
  <li><p><strong>[Update]</strong> as pointed in the answers, <code>$.keyup</code> give access to the value after update. However, not only it doesn't work for mouse cut/paste, but also fails if you press and hold a key, only releasing after a lot has been entered. Or, if combining <code>keydown</code> and <code>keyup</code> to save/restore the old value, it breaks if the user types too fast.</p></li>
  </ol>
  
  <p>None of the solutions above are error free or really safe cross-browsers. Are there better solutions out there, either ready-to-use or in-the-making? Seems to be a common problem, I've attempted to answer <a href="https://stackoverflow.com/q/9108878/520779">similar</a> <a href="https://stackoverflow.com/q/9204783/520779">questions</a> recently, with no success, and I'm interested in an answer as well.</p>
  
  <p>(A good argument <strong>against</strong> this practice is also welcome, in case there are new problems that would rise if this validation were implemented; however, that seems a common thing in other languages, so I doubt it is a bad thing to want)</p>