tags:

views:

39

answers:

1

Hi Folks, First of all I apologize, I am posting tons of questions will silly problems. This is very much the first time I am going to use JQuery heavily. So now my problem is, I have a text field, which gets input from a usb device (works as a keyboard wedge), as soon as this INPUT field is populated I want to make an AJAX call, sending the read value to server. I thought on using change event using the attached code, but it isnt working correctly reason being, because the input is 15 digit alpha numeric and populates one by one, just after the first character comes in, the change event triggers. What I want is, make the ajax call after the complete input is in the textfield, make the ajax call and then clear the text field for next input.

$(document).keydown(function() {
   $("#serialCode").focus();
   $("#serialCode").change(function() { 
       var $serialNumber = $("#serialCode").val().substring(0,9) ;
       alert($serialNumber);  
       $(this).val("");
    });
});
+1  A: 

I'd have a the keydown event on the text field first. within that event i'd be checking for the length of the field and when it reached 15 then do the post back

$("#serialCode").change(function() { 
       var $serialNumber = $("#serialCode").val().substring(0,9) ;
       //check the length here first
       var codeLen = $('.hiddenCodeLength').val();
       if ($serialNumber.length == codeLen)
       {
         alert($serialNumber);  
         $(this).val("");
       }
    });
griegs
HI griegs, something like this? if ($serialNumber.length == 15) {
t3ch
Something like that yeah. No point doing a post back until the minimum requirements are met huh?
griegs
Thats true, but its more like hardcoding, what if tomorrow the length of input changed to lets say 20.
t3ch
How about having the code length in a hidden field. see my code edit. .hiddenCodeLenth is a class applied to a hidden field <input type='hidden' class='hiddenCodeLength'> Then you can set it from code behind from a config file.
griegs
Hmmm... I guess if you make your INPUT hidden, it doesnt read from the keyboard wedge :(
t3ch
ah ok, i guess you need to come up with a way to get the key length into your field. you could set it as an attribute on the input field which comes from the wedge
griegs
Hmmm.. yeah something like that. not sure what lol
t3ch