onkeyup() event - possible to detect if key press was ignored because a field was at it's maxlength?
<p>I have a form field for entering a user id.   The user id is always 6 characters so the field is limited to a maxlength of 6 characters.  </p>

<p>The field has an onkeyup() event to call a funtion that looks up the user id and fills in several other form fields if the user id is valid.  Most people I know have used onblur() for something like this but I never liked how a user has to tab to or click on another field before the autofilling AJAX goes off.</p>

<p>The function right now will return w/o doing anything if the field length is &lt; 6 characters or if the key that was pressed is a left or right cursor field.   </p>

<p>There's one flaw left I haven't been able to think around.   Since the field is limited to 6 characters, if 6 characters are already entered and another key is pressed, the value of the field will not change but the function will fire and validate the field (un-necessary validation/db-query).</p>

<p>Is there anyway to prevent the function from going off in this case?   I'm thinking it's not possible but wanted to check.  I had a thought if the field length was 6 I could check the last key pressed against the 6th character of the field, but if someone typed something like 'a' as the 6th character and then 'a' again, it wouldn't work.  I guess that could eliminate all possibilities except for the one case though one case (not perfect but better).</p>

<p>Thanks!</p>

<pre><code>(rails) &lt;%= f.text_field :uid, :size =&gt; 10, :maxlength =&gt; 6, :class =&gt; 'validate_text', :onkeyup =&gt; uid_lookup (event)', :autocomplete =&gt; :off   %&gt;

&lt;script type="text/javascript"&gt;
    String.prototype.trim = function() {
      return this.replace(/^\s+|\s+$/g,"");
    }
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
        uid_lookup = function(e){
        var unicode=e.keyCode? e.keyCode : e.charCode;
        if (unicode == 37 || unicode == 39) {    // ignore a left or right arrow press
            return
        }

        var uid= $('uid').value;
        uid = uid.trim();
        $('uid).value = uid;   //uid's have no spaces, go ahead and remove if typed
        if (uid.length != 6) {
            return
        }
        // db lookup &amp; form autofill

        }
&lt;/script&gt;       
</code></pre>
