Converting Javascript

SMatthews

Registered User.
Local time
Today, 13:49
Joined
Nov 13, 2001
Messages
38
I have a javascript from a website that I would to implement into an access form if possible. The javascript consists of a formula that validates bank routing numbers.
Is there an easy way to implement or convert the script to a usable format? The code follows.

Thanks in advance.


function checkABA(s) {

var i, n, t;

// First, remove any non-numeric characters.

t = "";
for (i = 0; i < s.length; i++) {
c = parseInt(s.charAt(i), 10);
if (c >= 0 && c <= 9)
t = t + c;
}

// Check the length, it should be nine digits.

if (t.length != 9)
return false;

// Now run through each digit and calculate the total.

n = 0;
for (i = 0; i < t.length; i += 3) {
n += parseInt(t.charAt(i), 10) * 3
+ parseInt(t.charAt(i + 1), 10) * 7
+ parseInt(t.charAt(i + 2), 10);
}

// If the resulting sum is an even multiple of ten (but not zero),
// the aba routing number is good.

if (n != 0 && n % 10 == 0)
return true;
else
return false;
}

function validateForm(f) {

var s = f.elements["aba"].value;

if (checkABA(s))
alert("OK");
else
alert("INVALID");
return false;
}
 
SMathews,

The following code should do the trick. Pass the value you want validated into the below function and it will return true or false. I checked it against the Javascript version and received the same results, but haven't done a lot of testing.

HTH
SteveA
smile.gif


Code:
Function checkABA(ByRef txtABA As String)

' This program will convert an incoming string into a numeric value, and then confirm
' whether it is a valid ABA.

' If the validation is successful, the function will return true and the txtABA variable will
' be updated with the actual ABA.

' If the validation fails, the function will return false and the txtABA variable will not be
' modified.

Dim i As Integer, n As Long, t As String, c As String

' If the incoming field is less than 9 characters, the check has failed.
If Len(txtABA) < 9 Then
    checkABA = False
    Exit Function
End If

' Loop through each character in the string, and save off those that are numeric.
For i = 1 To Len(txtABA) Step 1
    ' Extract the individual character.
    c = Mid(txtABA, i, 1)
    ' Test the character and confirm it is numeric.
    If IsNumeric(c) Then
        ' If it is numeric add it to the saved string.
        t = t & c
    End If
Next

' If the new string is not nine characters long, the check has failed.
If Len(t) <> 9 Then
    checkABA = False
    Exit Function
End If

' Add the weightings to each digit.
n = 0
For i = 1 To 9 Step 3
    n = n + (Mid(t, i, 1) * 3)
    n = n + (Mid(t, (i + 1), 1) * 7)
    n = n + (Mid(t, (i + 2), 1))
Next

' If the number is zero, the check has failed
If n = 0 Then
    checkABA = False
    Exit Function
End If

' If the result mod 10 is not zero, the ABA is not valid.
If (n Mod 10) <> 0 Then
    ' Otherwise, the check has failed.
    checkABA = False
    Exit Function
End If

' The ABA is valid so update the sent variable txtABANumber with the correct ABA Number
txtABA = t
checkABA = True

End Function
P.S. You might want to add some error handling to support any unexpected errors.

NB. Made minor change to code.

[This message has been edited by SteveA (edited 01-25-2002).]
 

Users who are viewing this thread

Back
Top Bottom