/**
* 校验银行卡卡号
* @param cardId
* @return
*/ public static boolean checkBankCard(String cardId) { char bit = getBankCardCheckCode(cardId.substring(0, cardId.length() - 1)); if(bit == 'N'){ return false; } return cardId.charAt(cardId.length() - 1) == bit; } /**
* 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
* @param nonCheckCodeCardId
* @return
*/ public static char getBankCardCheckCode(String nonCheckCodeCardId){ if(nonCheckCodeCardId == null || nonCheckCodeCardId.trim().length() == 0 || !nonCheckCodeCardId.matches("\\d+")) { //如果传的不是数据返回N
return 'N'; } char[] chs = nonCheckCodeCardId.trim().toCharArray(); int luhmSum = 0; for(int i = chs.length - 1, j = 0; i >= 0; i--, j++) { int k = chs[i] - '0'; if(j % 2 == 0) { k *= 2; k = k / 10 + k % 10; } luhmSum += k; } return (luhmSum % 10 == 0) ? '0' : (char)((10 - luhmSum % 10) + '0'); } // 测试代码public class ResTest {
public static void main(String[] args) {
String reg = "18632116655";
String data = JsonUtil.toJson(reg);
String post = HttpClient.post("http://192.168.199.127:8067/openapi/rest/hometop/homeinfo", AESUtil.encrypt(data));
System.out.println(post);
System.out.println(AESUtil.decrypt(post));
}
}
/**
* author:qiaoyongjun
* StrNo:用户输入的身份证件号码
* _id:用来承载错误信息的控件ID号,用来进行友好提示
*判断身份证号码格式函数
*公民身份号码是特征组合码,
*排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码
*如果验证通过 返回 true
*/
function isChinaIDCard(StrNo, _Id) {
var error = document.getElementById(_Id);
StrNo = StrNo.toString();
if (StrNo.length == 15) {
if (!isValidDate("19" + StrNo.substr(6, 2), StrNo.substr(8, 2),
StrNo.substr(10, 2), _Id)) {
return false;
}
} else if (StrNo.length == 18) {
if (!isValidDate(StrNo.substr(6, 4), StrNo.substr(10, 2), StrNo
.substr(12, 2), _Id)) {
return false;
}
} else {
error.innerHTML = "";
error.innerHTML = "输入的身份证号码必须为15位或者18位!";
return false;
}
if (StrNo.length == 18) {
var a, b, c
if (!isNumber(StrNo.substr(0, 17))) {
error.innerHTML = "";
error.innerHTML = "身份证号码错误,前17位不能含有英文字母!";
return false;
}
a = parseInt(StrNo.substr(0, 1)) * 7 + parseInt(StrNo.substr(1, 1))
* 9 + parseInt(StrNo.substr(2, 1)) * 10;
a = a + parseInt(StrNo.substr(3, 1)) * 5
+ parseInt(StrNo.substr(4, 1)) * 8
+ parseInt(StrNo.substr(5, 1)) * 4;
a = a + parseInt(StrNo.substr(6, 1)) * 2
+ parseInt(StrNo.substr(7, 1)) * 1
+ parseInt(StrNo.substr(8, 1)) * 6;
a = a + parseInt(StrNo.substr(9, 1)) * 3
+ parseInt(StrNo.substr(10, 1)) * 7
+ parseInt(StrNo.substr(11, 1)) * 9;
a = a + parseInt(StrNo.substr(12, 1)) * 10
+ parseInt(StrNo.substr(13, 1)) * 5
+ parseInt(StrNo.substr(14, 1)) * 8;
a = a + parseInt(StrNo.substr(15, 1)) * 4
+ parseInt(StrNo.substr(16, 1)) * 2;
b = a % 11;
if (b == 2) //最后一位为校验位
{
c = StrNo.substr(17, 1).toUpperCase(); //转为大写X
} else {
c = parseInt(StrNo.substr(17, 1));
}
switch (b) {
case 0:
if (c != 1) {
error.innerHTML = "";
error.innerHTML = "身份证号码校验位错:最后一位应该为:1";
return false;
}
break;
case 1:
if (c != 0) {
error.innerHTML = "";
error.innerHTML = "身份证号码校验位错:最后一位应该为:0";
return false;
}
break;
case 2:
if (c != "X") {
error.innerHTML = "";
error.innerHTML = "身份证号码校验位错:最后一位应该为:X";
return false;
}
break;
case 3:
if (c != 9) {
error.innerHTML = "";
error.innerHTML = "身份证号码校验位错:最后一位应该为:9";
return false;
}
break;
case 4:
if (c != 8) {
error.innerHTML = "";
error.innerHTML = "身份证号码校验位错:最后一位应该为:8";
return false;
}
break;
case 5:
if (c != 7) {
error.innerHTML = "";
error.innerHTML = "身份证号码校验位错:最后一位应该为:7";
return false;
}
break;
case 6:
if (c != 6) {
error.innerHTML = "";
error.innerHTML = "身份证号码校验位错:最后一位应该为:6";
return false;
}
break;
case 7:
if (c != 5) {
error.innerHTML = "";
error.innerHTML = "身份证号码校验位错:最后一位应该为:5";
return false;
}
break;
case 8:
if (c != 4) {
error.innerHTML = "";
error.innerHTML = "身份证号码校验位错:最后一位应该为:4";
return false;
}
break;
case 9:
if (c != 3) {
error.innerHTML = "";
error.innerHTML = "身份证号码校验位错:最后一位应该为:3";
return false;
}
break;
case 10:
if (c != 2) {
error.innerHTML = "";
error.innerHTML = "身份证号码校验位错:最后一位应该为:2";
return false;
}
}
} else {//15位身份证号
if (!isNumber(StrNo)) {
error.innerHTML = "";
error.innerHTML = "身份证号码错误,前15位不能含有英文字母!";
return false;
}
}
return true;
}
/**
* 验证身份证件中的日期是否合法有效
* @param iY
* @param iM
* @param iD
* @param _id
* @return
*/
function isValidDate(iY, iM, iD, _id) {
if (iY > 2200 || iY < 1900 || !isNumber(iY)) {
document.getElementById(_id).innerHTML = "";
document.getElementById(_id).innerHTML = "输入身份证号,年度" + iY + "非法!";
return false;
}
if (iM > 12 || iM <= 0 || !isNumber(iM)) {
document.getElementById(_id).innerHTML = "";
document.getElementById(_id).innerHTML = "输入身份证号,月份" + iM + "非法!";
return false;
}
if (iD > 31 || iD <= 0 || !isNumber(iD)) {
document.getElementById(_id).innerHTML = "";
document.getElementById(_id).innerHTML = "输入身份证号,日期" + iD + "非法!";
return false;
}
return true;
}
/**
* 验证是否为数字
* @param oNum
* @return
*/
function isNumber(oNum) {
if (!oNum)
return false;
var strP = /^\d+(\.\d+)?$/;
if (!strP.test(oNum))
return false;
try {
if (parseFloat(oNum) != oNum)
return false;
} catch (ex) {
return false;
}
return true;
}