中国居民18位身份证号验证方法,Java算法实现
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');
}
}
网友评论