As mention on the other answer, bar-code scanner or card reader works like a keyboard. You can attach a listener, for example in the whole document:
document.onkeypress = function(e) {
e = e || window.event;
var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
// store it , in this example, i use localstorage
if(localStorage.getItem("card") && localStorage.getItem("card") != 'null') {
// append on every keypress
localStorage.setItem("card", localStorage.getItem("card") + String.fromCharCode(charCode));
} else {
// remove localstorage if it takes 300 ms (you can set it)
localStorage.setItem("card", String.fromCharCode(charCode));
setTimeout(function() {
localStorage.removeItem("card");
}, 300);
}
// when reach on certain length within 300 ms, it is not typed by a human being
if(localStorage.getItem("card").length == 8) {
// do some validation
if(passedonvalidation){
// do some action
}
}
}
You can attach listener on a textbox if you want to. Make sure it is focused when the card is swiped.