/**
* 银行卡校验规则
*
* @param bankNo
*
*/
public static boolean luhmCheck(String bankNo, Context context, Button btn) {
// UiToast toast = new UiToast(context, null);
// if (bankNo.length() 19) {
// toast.setStr("银行卡号长度必须在16到19之间");
// toast.show();
// }
int lastNum = Integer.parseInt(bankNo.substring(bankNo.length() - 1,
bankNo.length()));// 取出最后一位(与luhm进行比较)
String first15Num = bankNo.substring(0, bankNo.length() - 1);// 前15或18位
// System.out.println(first15Num);
char[] newArr = new char[first15Num.length()]; // 倒叙装入newArr
char[] tempArr = first15Num.toCharArray();
for (int i = 0; i
newArr[tempArr.length - 1 - i] = tempArr[i];
}
int[] arrSingleNum = new int[newArr.length]; // 奇数位*2的积 <9
int[] arrSingleNum2 = new int[newArr.length];// 奇数位*2的积 >9
int[] arrDoubleNum = new int[newArr.length]; // 偶数位数组
for (int j = 0; j
if ((j + 1) % 2 == 1) {// 奇数位
if ((int) (newArr[j] - 48) * 2
arrSingleNum[j] = (int) (newArr[j] - 48) * 2;
else
arrSingleNum2[j] = (int) (newArr[j] - 48) * 2;
} else
// 偶数位
arrDoubleNum[j] = (int) (newArr[j] - 48);
}
int[] arrSingleNumChild = new int[newArr.length]; // 奇数位*2 >9
// 的分割之后的数组个位数
int[] arrSingleNum2Child = new int[newArr.length];// 奇数位*2 >9
// 的分割之后的数组十位数
for (int h = 0; h
arrSingleNumChild[h] = (arrSingleNum2[h]) % 10;
arrSingleNum2Child[h] = (arrSingleNum2[h]) / 10;
}
int sumSingleNum = 0; // 奇数位*2
int sumDoubleNum = 0; // 偶数位数组之和
int sumSingleNumChild = 0; // 奇数位*2 >9 的分割之后的数组个位数之和
int sumSingleNum2Child = 0; // 奇数位*2 >9 的分割之后的数组十位数之和
int sumTotal = 0;
for (int m = 0; m
sumSingleNum = sumSingleNum + arrSingleNum[m];
}
for (int n = 0; n
sumDoubleNum = sumDoubleNum + arrDoubleNum[n];
}
for (int p = 0; p
sumSingleNumChild = sumSingleNumChild + arrSingleNumChild[p];
sumSingleNum2Child = sumSingleNum2Child + arrSingleNum2Child[p];
}
sumTotal = sumSingleNum + sumDoubleNum + sumSingleNumChild
+ sumSingleNum2Child;
// 计算Luhm值
int k = sumTotal % 10 == 0 ? 10 : sumTotal % 10;
int luhm = 10 - k;
if (lastNum == luhm) {
return true;
} else if (bankNo.length() 19) {
return false;
} else {
return false;
}
}