有一些功能,文本框只接受来自条形码扫描仪的输入,并限制来自键盘的任何其他输入。
答案 0 :(得分:2)
以下是限制键盘的输入..只需尝试连接条形码扫描仪并检查它是否有效..
textBox.onkeypress = function(e) {
e = e || window.event;
var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
if (/\D/.test(String.fromCharCode(charCode))) {
return false;
}
};
对于字母数字
Chk this
答案 1 :(得分:0)
在http://www.deadosaurus.com/detect-a-usb-barcode-scanner-with-javascript
上查看在链接中,我修改为在不符合10个字符长度标准时自动清除文本,这可以假设不符合10个字符长度标准=未从条形码扫描器输入。
我正在使用ASP.NET,下面是我的示例:
对于ASP代码:
<asp:TextBox ID="TextBoxComponentPartNumber" runat="server" onkeypress="AutoClearOrSetInputText(event,this.id);" ></asp:TextBox>
<asp:TextBox ID="TextBoxAssemblyPartNumber" runat="server" onkeypress="AutoClearOrSetInputText(event,this.id);" ></asp:TextBox>
对于JavaScript:
<script type="text/javascript">
//This variables is for AutoClearOrSetInputText function
var pressed = false;
var chars = [];
//This function will auto clear or set input text box’s text value
function AutoClearOrSetInputText(eventForTextBox,idForTextBox) {
// add each entered char to the chars array
chars.push(String.fromCharCode(eventForTextBox.which));
// variable to ensure we wait to check the input we are receiving
if (pressed == false) {
// we set a timeout function that expires after 0.5 sec, once it does it clears out a list
// of characters
setTimeout(function() {
// check we have a long length e.g. it is a barcode
if (chars.length >= 10) {
// join the chars array to make a string of the barcode scanned
var barcode = chars.join(“”);
// assign value to input for barcode scanner scanned value
document.getElementById(idForTextBox).value = barcode;
}
else {
// clear value from input for non-barcode scanner scanned value
document.getElementById(idForTextBox).value = ”;
}
chars = [];
pressed = false;
}, 500);
}
// set press to true so we do not reenter the timeout function above
pressed = true;
}
</script>