public static boolean validate18Idcard(String idcard){
if(idcard == null ) {
return false;
}
if(idcard.length()!=18) {
return false;
}
char [] id =idcard.toCharArray();
int i, sum, n;
for (sum = i = 0; i < 17; i++){
sum += ((1 << (17 - i)) % 11) * (id[i] - '0');
}
n = (12 - (sum % 11)) % 11;
if (n < 10) {
return (n == id[17] - '0');
} else {
return (id[17] == 'X');
}
}
了解更多,请看 维基百科:https://zh.wikipedia.org/wiki/%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD%E5%85%AC%E6%B0%91%E8%BA%AB%E4%BB%BD%E5%8F%B7%E7%A0%81