Use JavaScript to prevent spacebar?
<p>I have a live search on a site I'm developing. At the moment, it searches the MySql database after the first character is typed, and updates the search for each new character. When <kbd>Space</kbd> is pressed as the first character, it displays all entries in the database. I don't want it to do that. I have the following code that I found somewhere that prevents the <code>SPACE</code> character from being typed:</p>

<pre><code>$('input[type="text"]').keydown(function(e){
  var ignore_key_codes = [8,32];
  if ($.inArray(e.keyCode, ignore_key_codes) &gt;= 0){
  e.preventDefault();
 }
});
</code></pre>

<p>This does what it's meant to do, but not exactly what I want. This will prevent the space bar from working in the text input at all, what I require is that it only prevents the space bar if it's the first character being typed. For example, typing " apples" would prevent the space, but typing "apples oranges" wouldn't. </p>

<p>Is there anything I can try to achieve this?</p>
