实现营业执照注册号校验

简单的了解营业执照注册号,注册号是有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 您输入的营业执照注册号为空,请核对后再输!


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是一个 Oracle 营业执照代码校验函数的示例: ```sql CREATE OR REPLACE FUNCTION check_license_code(p_license_code IN VARCHAR2) RETURN VARCHAR2 IS v_license_code VARCHAR2(15) := UPPER(p_license_code); v_check_code CHAR(1); v_weight_array CONSTANT VARCHAR2(10) := '123456789ABCDEFGHJKLMNPQRTUWXY'; v_weight_num_array CONSTANT VARCHAR2(10) := '31791065432'; BEGIN -- 校验长度和字符合法性 IF LENGTH(v_license_code) <> 15 OR REGEXP_LIKE(v_license_code, '[^0-9A-Z]') THEN RETURN 'INVALID_LICENSE_CODE'; END IF; -- 计算校验码 v_check_code := v_weight_array(MOD(SUM(DECODE(SUBSTR(v_license_code, LEVEL, 1), v_weight_array, INSTR(v_weight_array, SUBSTR(v_license_code, LEVEL, 1)), NULL) * TO_NUMBER(SUBSTR(v_weight_num_array, LEVEL, 1))), 31) + 1); -- 检查校验码 IF SUBSTR(v_license_code, 15, 1) <> v_check_code THEN RETURN 'INVALID_CHECK_CODE'; ELSE RETURN 'VALID_LICENSE_CODE'; END IF; END; / ``` 该函数接收一个营业执照代码作为参数,返回一个字符串,表示校验结果。如果营业执照代码不符合规则,则返回 "INVALID_LICENSE_CODE",如果校验码不正确,则返回 "INVALID_CHECK_CODE",否则返回 "VALID_LICENSE_CODE"。使用该函数时,只需要传入营业执照代码即可。例如: ```sql SELECT check_license_code('123456789012345') FROM DUAL; -- 返回 'INVALID_LICENSE_CODE' SELECT check_license_code('1234567890123456') FROM DUAL; -- 返回 'INVALID_LICENSE_CODE' SELECT check_license_code('1234567890123456A') FROM DUAL; -- 返回 'VALID_LICENSE_CODE' SELECT check_license_code('1234567890123456B') FROM DUAL; -- 返回 'INVALID_CHECK_CODE' ``` 注意,在实际应用中,可能需要根据具体要求对该函数进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值