Git Product home page Git Product logo

barcode-scanner.js's Introduction

barcode-scanner.js

Javascript library to handle and identify data (keystrokes) entered by a barcode scanner. Barcode Scanner

Demo / Try it

You can try the library and if you scanner does work with it here: DEMO

FAQ

Please check if you question was already asked/solved here: Github Issues

How it works

A barcode scanner works like a keyboard, when you scan a specific barcode, the barcode scanner device types the text version of the barcode like a keyboard, but it types so fast (<50ms between keystrokes) that you can identify input that is done with a barcode scanner and handle it in a special way.

Default usage

A barcode scanner input is pasted to the last input/textarea with an 'data-barcode-scanner-target' attribute found in the dom and submits the closest form.

  <form>
    <input data-barcode-scanner-target type='text'></input>
  </form>
  • If scanner scans 'IT1232'
  • barcode-scanner.js inserts 'IT1232' into the input field
  • Afterwards it submits the surrounding form

Behaviour when user focuses an input field

If an input or textarea is focused (clicked) by the user, the barcode scanner input is always inserted to the focused input/textarea and the surrounding form is submitted.

  <form>
    <input name='first' type='text'></input>
    <input name='second' data-barcode-scanner-target type='text'></input>
  </form>
  • User focuses (clicks) the input with the name 'first'
  • Scanner scans 'IT1232'
  • barcode-scanner.js inserts 'IT1232' to the input with name "first" (as its focused)
  • Afterwards it submits the surrounding form

Disable auto submit

The auto submit of the surrounding form can be disabled.

  <form data-prevent-barcode-scanner-submit>
    <input data-barcode-scanner-target type='text'></input>
  </form>

or

  <form>
    <input data-barcode-scanner-target data-prevent-barcode-scanner-submit type='text'></input>
  </form>
  • If scanner scans 'IT1232'
  • barcode-scanner.js inserts 'IT1232' into the input field
  • Afterwards it does NOT submit the surrounding form, as it's disabled

Custom actions / More complex scenarios

In case you have more complex requirements, like multiple input fields where something has to decide in which input field the code has to be entered, custom actions is what you can use to solve it.

  BarcodeScanner.addAction("c (id)", function(id) {
    console.log(this); // the current input/textarea that is the barcode target
    console.log(id);  // 2121232
  });
  • If scanner scans 'c 2121232'
  • The registered action will be performed (as it matches the registered pattern)
  • No form is getting submitted, no chars will be added to any input field

Structured Barcodes / Custom actions

If your barcode contains structured information, like which type of item is it etc., it's recommended to structure you barcode. This would allow this library to identify each part of the barcode. Like 'c 123 buy 5' where 'c' stands for custom action, '123' is the id of the item, buy the action you want to perform and '5' the amount.

  BarcodeScanner.addAction("c (id) (action) (amount)", function(id, action, amount) {
    console.log(this);    // the current input/textarea that is the barcode target
    console.log(id);      // 2121232
    console.log(action);  // print
    console.log(amount);  // 2
  });
  • If scanner scans "c 2121232 print 2"
  • The registered action will be performed (as it matches the registered pattern)
  • No form is getting submitted, no chars will be added to any input field

Tests

Jasmine Tests

barcode-scanner.js's People

Stargazers

Ivan avatar  avatar Erfan Reed avatar Savino Basanisi avatar Frogsuite IRD avatar Andrew Sokolov avatar Miftah Afina avatar vidy avatar Triantafyllos Paschaleris avatar Evagoras Charalambous avatar  avatar Alex avatar Fionna Yao avatar David Cox avatar Max Albrecht avatar Sebastian avatar Jeremy Seitz avatar

Watchers

Sebastian avatar  avatar  avatar  avatar  avatar Ertugrul Ozpolat avatar

barcode-scanner.js's Issues

multiple scans, listbox trigger submit and trigger event to AJAX.

Hello, this is great little nugget, ๐Ÿ‘ love the lib. I am trying to use it with my web app. (with the current version of Jquery it sometimes does not scan, can you please update).

`



$("input:first").focus()` 1. I have a scenario where the user may scan one, **or many scans** like a shopping cart, so I have put it in a `textbox` or `listbox` will the target work with a listbox. How can I achieve this where each item is separate. 2. If I have 2 input boxes, how does the scan know which one to put it in. 3. Can this also scan characters, and which libs are supported. For e.g. if I have 2 types of codes Parts `P0938823` and Fluids `F0238847` would the scanner pick up the P and F 4. how does it know which barcode standard to use. I mean is this a configuration in the lib or the scanner prefix codes. 5. how can I trigger an Ajax silent `POST` on every scan. is it possible? 6. Is this auto submitted when I hit scan? I do not see a submit, or is it a separate user action, where they click submit button.

thanks

Disconnect, Offline storage and retry

Dear Pape thanks for this wonderful code.

I am trying your lib because of its developer friendlier , for me internet connectivity is not very reliable, so its causing some issues.

  • Could you please add/show an option to dual-save the scans offline or local storage in-case the browser disconnects. Or share a small sample on how I could achieve this in your lib.
  • Is there a debug/option to show the scanner is in ready/online or acquired mode. I have an indicator/textbox ontop, which I change to green when the scanner is connected.

thanks

Disconnect/Offline storage

Asked by @unitedbummersconference here: #3 (comment)

Hi, for me on disconnects

 <form>
 <input data-barcode-scanner-target type='text'   class="pape-save-offline-barcode"></input>
  </form>
        $('form').on('submit', function(e){
        e.preventDefault();
        SaveToOffline();
        SeverBatchUpload();
    });

totally bombs out on me,would be supeer sweet, if this was an option

Feature: 2D

If we have a 2D scanner, will this lib work

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.