iedarbīgas zāles ([info]bez4pieci) rakstīja [info]koderi kopienā,
īsumā: ir kaut kāda gara simbolu virkne, kas tiek iepeistota input laukā, un no kuras jāizdabū simbolu virkne, kas jāatstāj tajā laukā, bet pārējais viss garbage vienkārši jāizmet.
vajadzētu sagalabāt re-usability..

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) { 
		}
	}

});


(Lasīt komentārus)

Nopūsties:

No:
Lietotājvārds:
Parole:
Ievadi te 'qws' (liidzeklis pret spambotiem):
Temats:
Tematā HTML ir aizliegts
  
Ziņa:

Gandrīz jau aizmirsu pateikt – šis lietotājs ir ieslēdzis IP adrešu noglabāšanu. Operatore Nr. 65.
Neesi iežurnalējies. Iežurnalēties?