如何检测键盘楔形扫描器条形码输入到Web输入和阻止键盘输入

时间:2017-01-24 10:14:15

标签: javascript html forms barcode

我需要在网页表单的文本字段中强制执行条形码扫描程序,我必须阻止键盘或粘贴条目。这适用于个人用户拥有自己的条形码卡的制造环境,不允许他们预订其他人的工作,这将是作弊!他们扫描的代码是他们的员工编号,总是4个字符。条形码扫描仪是USB,表现为键盘依次输入每个字符。

1 个答案:

答案 0 :(得分:1)

我提出的代码检测到字符1和4之间的时间。如果它低于10毫秒,则代码被扫描条形码,如果它是键盘输入的时间更长。如果粘贴了代码,那么该字段的长度永远不会超过1个字符,因此未设置定时器变量,时间再次超过10毫秒。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>

<script>
    var startTime = new Date().getTime();

  function barcodeScanCheck(){
    var input = document.getElementById("barcode").value;

    if(input.length == 1){
      startTime = new Date().getTime();
    }
    else if(input.length == 4){
      var elapsedTime = new Date().getTime() - startTime;
      if(elapsedTime < 10){
        alert("Barcode");
      }
      else{
        document.getElementById("barcode").value = "";
        alert("Keyboard");
      }
    }
  }
</script>

</head>

<body>

<form action="input.html" method="get">
<input id="barcode" name="barcode" type="text" onInput="barcodeScanCheck()">
<input name="submit" type="submit">
</form>

</body>
</html>