开发者

JS validating barcode scanner data too early

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开发者_如何学JAVA 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!


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 ;)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜