Scanning a barcode with an ASCII control character into an input field

Go To


I need to scan a barcode with ASCII 29 (group separator) into an HTML input field, and replace all the group separators with |. The following JavaScript function works when I first scan the barcode into Notepad++ and then copy it to the input field, but not when the barcode is scanned directly into the input field. What's the problem?

var barcode = document.getElementById('barcode').value;
barcode = barcode.replace(new RegExp(String.fromCharCode(29), 'g'), '|');
2012-04-04 05:35
by John Smith
Probably a browser limitation. Which browsers have you tested - Andrew Leach 2012-04-04 07:23
Firefox 11 and IE 8 - John Smith 2012-04-04 07:25
Can you use the browser dev tools to inspect the value of the data being put into the html input field after you scan a bar code and verify that it has ASCII 29 values in it - Shane Wealti 2012-04-04 12:12
I'm guessing that the browser is stripping out control codes in that input field - Shane Wealti 2012-04-06 14:23


On my Symbol Tech barcode scanner, characters are sent as key strokes. For example, the group separator will emulate holding down the left_control key and then sending a right bracket. Your browser will handle the simulated key strokes as if you were trying to use CTRL+] as a shortcut.

You can capture this event with onkeydown and event.which as described here: Firefox onkeydown detect pressed key

2014-11-05 01:12
by infinitenothing