user2450398
user2450398

Reputation: 195

allow textbox input from barcode scanner only and restrict any input from keyboard

There is functionality like, the textbox accepts input from only barcode scanner and restricts any other input from keyboard.

Upvotes: 2

Views: 7861

Answers (2)

LifeiSHot
LifeiSHot

Reputation: 139

Check this out at http://www.deadosaurus.com/detect-a-usb-barcode-scanner-with-javascript

From the link, I have modified to auto clear the text when it is not meeting 10 character length criteria, which can assume as not meeting 10 character length criteria = not input from barcode scanner.

I am using ASP.NET, below is my sample:

For ASP code:

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

For 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> 

Upvotes: 0

Vaibs_Cool
Vaibs_Cool

Reputation: 6156

Following is for restricting input from keyboard .. Just try connecting your barcode scanner and check if it works..

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

LIVE DEMO

For Alphanumeric

Chk this

LIVE DEMO

Upvotes: 1

Related Questions