# Check-digit Algorithm ABA Routing Number

Can anyone tell me why this vb.net check-digit algorithm is not returning the correct 'ckdigit' for a 9 digit ABA Routing Number?

For i = 1 To Len(Trim(txtRoutNum.Text)) - 1

pos(i - 1) = Mid(txtRoutNum.Text, i, 1)
If i = 1 Or i = 4 Or i = 7 Then
pos(i - 1) = pos(i - 1) * 3
ElseIf i = 2 Or i = 5 Or i = 8 Then
pos(i - 1) = pos(i - 1) * 7
Else
'do nothing
End If
posSum = posSum + pos(i - 1)

Next i

If posSum Mod 10 <> 0 Then
posRound = (posSum - (posSum Mod 10)) + 10
Else
posRound = posSum
End If

ckDigit = posRound - posSum

Thanks,
Dan
###### Who is Participating?

Middle School Assistant TeacherCommented:
Based on the article here:
http://www.brainjar.com/js/validation/

I came up with:

Private Function IsValidRoutingNumber(ByVal number As String) As Boolean
Dim validNumber As Boolean = False ' assume the worst until proven otherwise
If number Like "#########" Then
Dim digit As Integer
Dim sum As Integer = 0
For i As Integer = 0 To number.Length - 1
digit = Integer.Parse(number.Substring(i, 1))
Select Case i
Case 0, 3, 6
sum += (digit * 3)
Case 1, 4, 7
sum += (digit * 7)
Case 2, 5, 8
sum += digit
End Select
Next
If (sum <> 0) AndAlso (sum Mod 10 = 0) Then
validNumber = True
End If
End If
Return validNumber
End Function

So you can just call this function from within your validating event and act accordingly.

Also, with the number "307083694", tranposing the last two digits gives us "307083649" which is still a valid number.
0

You'll have to define "correct"
0

Middle School Assistant TeacherCommented:
Can you please give us a couple examples of ABA Routing Numbers along with the correct "ckdigit" for each?

It looks like this is VB6 code copied over for VB.net.  You might have an "off by one" error.

Did you really want to omit the last character from the For loop?

For i = 1 To Len(Trim(txtRoutNum.Text)) - 1

If you want to include the last character then it should be:

For i = 1 To Len(Trim(txtRoutNum.Text))

You usually need the " - 1" part when you start off with 0 (zero), not one.  The VB.Net string maniulation functions are zero based while the VB6 legacy functions are still one based.
0

Author Commented:
The valid routing number I am using is 307083694.

Here is my complete code.

Private Sub txtRoutNum_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRoutNum.Validating

Dim pos(8) As Integer
Dim i As Integer
Dim posSum As Integer
Dim posRound As Integer
Dim ckDigit As Integer
Dim RoutNum As String
Dim ckRoutNum As String

RoutNum = txtRoutNum.Text
ckRoutNum = Mid(RoutNum, 9, 1)

If Not IsNothing(txtRoutNum) Then

If Not (txtRoutNum.Text Like "#########") Then

e.Cancel = True

txtRoutNum.Select(0, txtRoutNum.Text.Length)

epRoutNumb.SetError(txtRoutNum, "Please verify number of digits = 9.")

txtRoutNum.Focus()

Exit Sub

Else

epRoutNumb.SetError(txtRoutNum, "")

End If

For i = 1 To Len(Trim(txtRoutNum.Text))

pos(i - 1) = Mid(txtRoutNum.Text, i, 1)
If i = 1 Or i = 4 Or i = 7 Then
pos(i - 1) = pos(i - 1) * 3
ElseIf i = 2 Or i = 5 Or i = 8 Then
pos(i - 1) = pos(i - 1) * 7
Else
'do nothing
End If
posSum = posSum + pos(i - 1)

Next i

If posSum Mod 10 <> 0 Then
posRound = (posSum - (posSum Mod 10)) + 10
Else
posRound = posSum
End If

ckDigit = posRound - posSum

If ckDigit <> 0 Then

'If ckDigit <> RTrim(txtRoutNum.Text, 1) Then
'If CStr(ckDigit) <> txtRoutNum.Text.Substring(9) Then
If CStr(ckDigit) <> ckRoutNum Then
MsgBox("Not a valid routing number.")
e.Cancel = True
Exit Sub
End If

sqlconMSB8.Open()

("Select RIO_ROUTNUMB, RIO_SHORTNAME From RIOCHEQ00100" _
& "Where RIO_ROUTNUMB ='" & txtRoutNum.Text & "'", sqlconMSB8)

Dim myTable As New DataTable
Dim myRow As DataRow

myRow = myTable.Rows(0)

If IsNothing(myRow("RIO_ROUTNUMB")) Then
MsgBox(" " & txtBankName.Text & " does not participate in the ACH program." _
& Chr(13) & "You must select another method of payment.")
e.Cancel = True
End If

txtBankName.Text = myRow("RIO_SHORTNAME")

sqlconMSB8.Close()

End If

Else
Close()
End If

End Sub

Do I have to remove the -1 from all of the check digit code?  With the code I have above, if I transpose the last two numbers and I just remove the -1 from
For i = 1 To Len(Trim(txtRoutNum.Text)) - 1,
my ckDigit is 0, and the invalid routing number msg is not returned.  The transposed number is interpruted as valid.
Thanks,
Dan
0

Author Commented:
THANK YOU SO MUCH.  I can not go-live with my application and hit my deadline.
Dan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.