tags:

views:

38

answers:

2

Hello all,

I am using a barcode scanner to enter in data and then send the code back to the server to validate it against the database. I'm having a problem of the javascript kicking in after the first character is entered. I need to find a way to delay this so the full 19 charaters can be entered. The barcode I am trying to check contains numbers, spaces, and dashes.

Here is what I have so far:

<form name="getBarcode" method="GET">
Barcode: <input id = "barcodeEntry" type="text" name="barcode" onkeyup = "checkBarCode()" /><br />
</form>

and the JS

var checkBarCode = function() {

    var barcode = $("#barcodeEntry").val();

    if (barcode.length == 19) {
        var url = "check/";
        var data = {barcode:barcode};
        var args = {type:"GET", url:url, data:data, complete: checked}; 
        $.ajax(args);

        return false;
    } else {
        alert("something went wrong here");

        return false;
    }
}

var checked = function(res, status) {
    if (status == "success") {
        alert("Valid");
        return false;
    } else {
        alert("Not Valid");
        return false;
    }
}

Thanks in advance for any possible help!

+1  A: 

You shouldn't do an alert when the code's length is not 19, because it stops the scanner entering the chars in the input. Why do you return false in checkBarCode ?

Does the barcode scanner you use insert an enterchar at the end ?

PS: I'm currently working with barcodes and JS ;)

Fabien Ménager
Thank you, quick fix :)The barcode scanner I am using to test with doesn't add an enter char at the end but I have been informed that there will be some scanners that do as default.
khop
+1  A: 

Remove the else statement and it should work.

var checkBarCode = function() {

    var barcode = $("#barcodeEntry").val();

    if (barcode.length == 19) {
        var url = "check/";
        var data = {barcode:barcode};
        var args = {type:"GET", url:url, data:data, complete: checked}; 
        $.ajax(args);
    } 
}

And I don't think you need to return anything.

qw3n