iedarbīgas zāles (bez4pieci) rakstīja koderi kopienā, |
var CardInputCheck = new Class({ Implements: [ Options, Events ], element: null, options: { readerOutputLength: 117, // number of characters in the card reader output readerOutputRE: /^\%B\d{16}\^\s+\^\d+\?;\d+=\d+\?$/, // regexp for the card reader output focusOnSuccess: null, // element to focus on success onSuccess: $empty // function to execute on success }, initialize: function(inputFieldElement, options) { this.setOptions(options); this.element = $(inputFieldElement); // focus the input field in question, so when the card is swiped, the output is written in this input field if(this.element && $type(this.element) == 'element') { this.element.addEvent('keyup', this.elementOnKeyUp.bindWithEvent(this)); this.focusElement(); // for firefox (the delayed focus does not work for some reason) this.focusElement.delay(200, this); // for all other browsers, who are not ready for immediate focusing } }, elementOnKeyUp: function(event) { var v = this.element.get('value').trim(); if(v.length == this.options.readerOutputLength && v.test(this.options.readerOutputRE)) { this.element.set('value', v.substr(8, 10)); // extract the actual card number from the reader's output this.success(); } }, success: function() { if(this.options.focusOnSuccess && this.options.focusOnSuccess.focus) { try { this.options.focusOnSuccess.focus(); } catch(e) { } } this.fireEvent('success'); }, focusElement: function() { try { // focus method for some reason sometimes throws errors... this.element.focus(); } catch(e) { } } });
Nopūsties: