如何在java中验证银行路由号码?谁能帮我吗。例如
void boolean validate(String str){
// some code
return true; //if valid otherwise return false
}
我得到了解决方案。银行路由号码使用这种简单的方法进行验证。
public boolean validateRoutingNumber(String s) {
int checksum=0, len=0, sum=0, mod = 0;
len = s.length();
if(len != 9){
return false;
}else {
String newString = s.substring(s.length()-1);
checksum = Integer.parseInt(newString);
sum = (7*(Integer.parseInt(""+s.charAt(0))+Integer.parseInt(""+s.charAt(3))+ Integer.parseInt(""+s.charAt(6)))) +
(3*(Integer.parseInt(""+s.charAt(1))+Integer.parseInt(""+s.charAt(4))+ Integer.parseInt(""+s.charAt(7))))+
(9*(Integer.parseInt(""+s.charAt(2))+Integer.parseInt(""+s.charAt(5))));
mod = sum % 10;
if(mod == checksum)
return true;
else
return false;
}
}
为了好玩,我写了一个Angular js指令来做同样的事情:
angular.module('ldPortal.directives')
.directive('routingnumber', function(){
return {
// only use as an attribute
restrict: 'A',
require: 'ngModel',
link: function(scope, elem, attr, ctrl){
var regex = new RegExp('^[0-9]{9}$','');
var num_at = function(str, index){
try {
return parseInt(str.charAt(index))
}catch (execption){
console.write(execption);
}
};
var validate = function(value){
if(! (value)){
return false;
}
var strVal = value.toString();
if (!regex.test(strVal)){
return false;
}
var checksum = parseInt(strVal.substr(strVal.length-1));
var sum = 0;
sum += 7 * (num_at(strVal, 0)+num_at(strVal, 3)+num_at(strVal,6));
sum += 3 * (num_at(strVal, 1)+num_at(strVal, 4)+num_at(strVal, 7));
sum += 9 * (num_at(strVal, 2)+num_at(strVal, 5));
var mod = sum % 10;
return checksum == mod;
};
ctrl.$parsers.push(function(value){
var is_valid = validate(value);
ctrl.$setValidity('routingnumber', is_valid);
return is_valid ? value : undefined;
});
ctrl.$formatters.push(function(value){
var is_valid = validate(value);
ctrl.$setValidity('routingnumber', is_valid);
return value;
});
}
};
});
这是python:
def validate_routing_number(routing_number):
# quick easy validations
if routing_number is None or routing_number == '':
return False
invalid_routing_message = 'invalid routing number'
if not re.match(r'^[0-9]{9}$', routing_number):
return False
# here is the more complicated validation!
checksum = int(routing_number[-1]) # last digit
sum = 0 # http://en.wikipedia.org/wiki/Routing_transit_number
sum += 7 * (int(routing_number[0])+int(routing_number[3])+int(routing_number[6]))
sum += 3 * (int(routing_number[1])+int(routing_number[4])+int(routing_number[7]))
sum += 9 * (int(routing_number[2])+int(routing_number[5]))
mod = sum % 10
return checksum != mod
我创建了一个简单的路由验证器,希望这个工作。
RoutingNumberValidator:import static java.lang.Character.getNumericValue;
public static class RoutingNumberValidator {
public boolean isValid(String value) {
boolean isValid = value != null && value.matches("[0-9]{9}");
if (isValid) {
int check = 0;
for ( int index = 0; index < 3; index++ ) {
int pos = index * 3;
check += getNumericValue(value.charAt(pos)) * 3;
check += getNumericValue(value.charAt(pos + 1)) * 7;
check += getNumericValue(value.charAt(pos + 2));
}
isValid = check != 0 && check % 10 == 0;
}
return isValid;
}
}
路由号码验证器测试:
public class RoutingNumberValidatorTest {
private List<String> validRoutingNumbers = Arrays.asList("122105155", "082000549");
private List<String> invalidRoutingNumbers = Arrays.asList("1232101155", "032000549");
@Test
public void isValid() throws Exception {
RoutingNumberValidator routingNumberValidator = new RoutingNumberValidator();
validRoutingNumbers.forEach(it-> assertThat(routingNumberValidator.isValid(it)).as("Invalid Routing Number should not be valid %s", it).isTrue());
invalidRoutingNumbers.forEach(it-> assertThat(routingNumberValidator.isValid(it)).as("Invalid Routing Number should not be valid %s", it).isFalse());
}
}
package RoutingNumberAlgo;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int checksum = 0, len = 0, sum = 0, mod = 0, rem = 0;
Scanner ss = new Scanner(System.in);
System.out.print("Enter the Routing Number: ");
String s = ss.nextLine();
len = s.length();
if (len != 9) {
System.out.print("Length not 9");
} else {
String newString = s.substring(s.length() - 1);
checksum = Integer.parseInt(newString);
sum = (3 * (Integer.parseInt("" + s.charAt(0)))) + (7 * (Integer.parseInt("" + s.charAt(1))))
+ (1 * (Integer.parseInt("" + s.charAt(2)))) + (3 * (Integer.parseInt("" + s.charAt(3))))
+ (7 * (Integer.parseInt("" + s.charAt(4)))) + (1 * (Integer.parseInt("" + s.charAt(5))))
+ (3 * (Integer.parseInt("" + s.charAt(6)))) + (7 * (Integer.parseInt("" + s.charAt(7))));
mod = 10 - (sum % 10);
if (mod == checksum)
System.out.print("True");
else
System.out.print("False");
;
}
}
}