JAvaは18ビットの身分証明書番号が合法かどうかを検証します
2459 ワード
身分証明書番号をパラメータとして判断します.
/**
* 18
*
* @param idCard
*
* @return
*/
/** 。 */
public static boolean validateIdCard18(String idCard) {
boolean bTrue = false;
if (idCard.length() == 18) {
// 17
String code17 = idCard.substring(0, 17);
// 18
String code18 = idCard.substring(17, 18);
if (isNum(code17)) {
char[] cArr = code17.toCharArray();
if (cArr != null) {
int[] iCard = converCharToInt(cArr);
int iSum17 = getPowerSum(iCard);
//
String val = getCheckCode18(iSum17);
if (val.length() > 0) {
if (val.equalsIgnoreCase(code18)) {
bTrue = true;
}
}
}
}
}
return bTrue;
}
/**
*
*
* @param val
* @return 。
*/
public static boolean isNum(String val) {
return val == null || "".equals(val) ? false : val.matches("^[0-9]*{1}");
}
/**
*
*
* @param ca
*
* @return
*/
public static int[] converCharToInt(char[] ca) {
int len = ca.length;
int[] iArr = new int[len];
try {
for (int i = 0; i < len; i++) {
iArr[i] = Integer.parseInt(String.valueOf(ca[i]));
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
return iArr;
}
/**
* ,
*
* @param iArr
* @return 。
*/
public static int getPowerSum(int[] iArr) {
int iSum = 0;
if (power.length == iArr.length) {
for (int i = 0; i < iArr.length; i++) {
for (int j = 0; j < power.length; j++) {
if (i == j) {
iSum = iSum + iArr[i] * power[j];
}
}
}
}
return iSum;
}
/** */
public static final int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
/**
* power 11
*
* @param iSum
* @return
*/
public static String getCheckCode18(int iSum) {
String sCode = "";
switch (iSum % 11) {
case 10:
sCode = "2";
break;
case 9:
sCode = "3";
break;
case 8:
sCode = "4";
break;
case 7:
sCode = "5";
break;
case 6:
sCode = "6";
break;
case 5:
sCode = "7";
break;
case 4:
sCode = "8";
break;
case 3:
sCode = "9";
break;
case 2:
sCode = "x";
break;
case 1:
sCode = "0";
break;
case 0:
sCode = "1";
break;
}
return sCode;
}