-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisabarouting.go
35 lines (28 loc) · 938 Bytes
/
isabarouting.go
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
package validatorgo
import (
"strconv"
"unicode/utf8"
)
// A validator that checks if the string is an ABA routing number for US bank account / cheque.
//
// ok := govalidator.IsAbaRouting("123456789")
// fmt.Println(ok) // false
// ok := govalidator.IsAbaRouting("021000021")
// fmt.Println(ok) // true
func IsAbaRouting(str string) bool {
strWithoutDashes := stripDashesAndSpaces(str)
if utf8.RuneCountInString(strWithoutDashes) != 9 || !IsNumeric(strWithoutDashes, &IsNumericOpts{NoSymbols: true}) {
return false
}
digits := make([]int, 9)
for i, char := range strWithoutDashes {
digit, _ := strconv.Atoi(string(char))
// err is ignore because we are assured IsNumericAbove to block all non numbers
// if err != nil {
// return false
// }
digits[i] = digit
}
checksum := 3*(digits[0]+digits[3]+digits[6]) + 7*(digits[1]+digits[4]+digits[7]) + 1*(digits[2]+digits[5]+digits[8])
return checksum%10 == 0
}