简单的了解营业执照注册号,注册号是有15位数字组成。简单的认识一下15数字的含义:代码结构工商注册号由14位数字本体码和1位数字校验码组成,其中本体码从左至右依次为:6位首次登记机关码、8位顺序码,1位数字校验码组成。
具体详情请点击:工商行政管理注册号编码规则
具体的校验代码实现如下:
/**
* 15位营业执照注册号检验工具
*/
public class BusinessUtils {
public static String ERROR_COMMON = "您输入的营业执照注册号%s,请核对后再输!";
/**
* 校验15位的营业执照注册号
*
* @param businessLicense
* @return
*/
public static String isBusinesslicense15(String businessLicense) {
String result = "";
if ("".equals(businessLicense) || " ".equals(businessLicense)) {
result = String.format(ERROR_COMMON,"为空");
} else if (businessLicense.length() != 15) {
result = String.format(ERROR_COMMON,"位数有误");
}else if (isBusinessLicense(businessLicense)) {// 传入15位 只校验营业执照的有效性推荐用这个
result = "true";
}else {
result = String.format(ERROR_COMMON,"有误");
}
return result;
}
/**
* 获取 营业执照注册号的校验码
*
* @param businessLicense 为15为返回1为有效,否则无效;传入14为则会计算出第15位的校验码。
* @return
*/
private static int getCheckCode(String businessLicense, boolean getCheckCode) {
int result = -1;
if (null == businessLicense || businessLicense.trim().equals("")|| businessLicense.length() != 15) {
return result;
}else{
int ti = 0;
int si = 0; // pi|11+ti
int cj = 0; // (si||10==0?10:si||10)*2
int pj = 10; // pj=cj|11==0?10:cj|11
for (int i = 0; i < businessLicense.length(); i++) {
ti = Integer.parseInt(businessLicense.substring(i,i+1));
si = pj + ti;
cj = (0 == si % 10 ? 10 : si % 10) * 2;
pj = (cj % 11) == 0 ? 10 : (cj % 11);
if (i == businessLicense.length()-2 && getCheckCode) {
result = (1 - pj < 0 ? 11 - pj : 1 - pj) % 10;// 返回营业执照注册号的校验码
return result;
}
if (i == businessLicense.length()-1) {
result = si % 10; // 返回1 表示是一个有效营业执照号
}
// System.out.println(i + " ti=" + ti + ", si=" + si + ", cj=" + cj + ", pj=" + pj);
}
}
return result;
}
private static boolean isBusinessLicense(String businessLicense) {
return 1 == getCheckCode(businessLicense, false);
}
/**
*
* @param businessLicense
* @return
*/
public static int getComputeCheckCode(String businessLicense){
return getCheckCode(businessLicense,true);
}
}
测试:
@Test // 如下测试营业执照百度的15位:
public void testRegexBusiness() {
String[] test = {
// 有效数据
"310117003171688", "330282602082220", "430524600099555",
"350205200033663", "320507000045918", "440301104041144",
"440307811872564", "130903000004802", "370125200024527",
"330225000051892", "340100000400528", "445381600190918",
"441900001868015", "310230000419464", "440301103072002",
"320483000067847", "110108000000016", "320300000174110",
// 错误测试数据
"320483000067848", "32048300006784", "3204830000678481", ""
};
for (int i = 0; i < test.length; i++) {
String temp = test[i];
System.out.println(temp + " 校验码:" + BusinessUtils.getComputeCheckCode(temp) + " " + BusinessUtils.isBusinesslicense15(temp));
}
}
测试结果:
310117003171688 校验码:8 true
330282602082220 校验码:0 true
430524600099555 校验码:5 true
350205200033663 校验码:3 true
320507000045918 校验码:8 true
440301104041144 校验码:4 true
440307811872564 校验码:4 true
130903000004802 校验码:2 true
370125200024527 校验码:7 true
330225000051892 校验码:2 true
340100000400528 校验码:8 true
445381600190918 校验码:8 true
441900001868015 校验码:5 true
310230000419464 校验码:4 true
440301103072002 校验码:2 true
320483000067847 校验码:7 true
110108000000016 校验码:6 true
320300000174110 校验码:0 true
320483000067848 校验码:7 您输入的营业执照注册号有误,请核对后再输!
32048300006784 校验码:-1 您输入的营业执照注册号位数有误,请核对后再输!
3204830000678481 校验码:-1 您输入的营业执照注册号位数有误,请核对后再输!
校验码:-1 您输入的营业执照注册号为空,请核对后再输!