Chris Nizzardini, Salt Lake City Utah, Web Developer Specializing in LAMP+Ajax Since 2006

My Blog

Here is my awesome blog.

JavaScript snippet for handling EFT check scanners

This piece of code will separate out the account number and routing number from a scanned check. I yanked this function out of a payment class I wrote for a point of sale web application I recently developed. It’s a mootools class, but the core logic can be pulled out and applied pretty much anywhere.

  1. First pass a string into the function, where the string is the string from the check scan
  2. I immediately validate the information is correct. Scanners will send a series of question marks if the check was scanned in at an odd angle. They are very picky, but I tested many ways of sending the check through and this should catch most scenarios
  3. The routing number is stored between two “T” characters in the string. So I look for the T’s, remove them and store off the routing number
  4. The end of the accounting number has a “U” character immediately after it so and the account number starts immediately after the routing number, so I substring that out
  5. I then do some more error checking. Since both the account and routing number should be numeric I use isNan() to verify this.
  6. The entire snippet is wrapped in a try catch so should anything fail it can be logged and reported to the end user
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
	setCheckAttributes: function(string){
		try{
			if(string.charAt(0)=='?' || string.charAt(1)=='?' || string.charAt(1)=='?')
			{
				displayMessageWindow('Invalid Check','Please verify the check was scanned properly with the check facing in and the routing/account numers at the bottom','error');
				return false;
			}
 
			var t1=string.search("T"); // start of routing number indentifier
			string = string.replace("T","");
			var t2=string.search("T"); // end of routing number indentifier, start of account number indentifier
 
			this.routing = string.substring(t1,t2); // set routing number
 
			var u=string.lastIndexOf("U"); // end of account number indentifier
 
			var account = string.substring((t2+1),u);
			var accountArr = account.split(" ");
 
			this.account = accountArr.join(""); // set account number
 
			if(isNaN(this.account) || isNaN(this.routing)){
				this.account='';
				this.routing='';
				this.payment_method_id=0;
				this.type='';
				displayMessageWindow('Invalid Check','The system encountered an error reading the check and has invalid (non-numeric) information for either the routing or account number.','error');
				return false;
			}
			return true;
		}
		catch(err){
			 pushToErrorLog('class Payment::setCheckAttributes() ' + err);
			 return false;
		}

To determine when a check has been scanned you can use the code in my post, JavaScript snippet for handling credit card readers.

Leave a Reply