java以及JavaScript的香港身份z验证方法。

java以及JavaScript的香港身份z验证方法。,第1张

java以及JavaScript的香港身份z验证方法。 Java版

    public static boolean validateHKCard(String idCard) {
        String verifyCode[] = {"1","2","3","4","5","6","7","8","9"};//第九位校验码(除A以外,A单独处理)
        //        String card = idCard.replaceAll("[\(|\)]", "");//如果输入带括号则取消注释,注释掉下一行
        String card = idCard;
        int sum = 0;
        char[] chars1 = card.substring(0, 1).toUpperCase().toCharArray();
        if (card.length() == 9) {
            sum = ((int) chars1[0] - 55) * 9
                    + ((int) card.substring(1, 2).toUpperCase().toCharArray()[0] - 55) * 8;
            card = card.substring(1, 9);
        } else {
            sum = 324 + ((int) chars1[0] - 55) * 8;
        }
        String mid = card.substring(1, 7);
        String end = card.substring(7, 8);
        char[] chars = mid.toCharArray();
        int iflag = 7;
        for (char c : chars) {
            sum = sum + Integer.parseInt(c + "") * iflag;
            iflag--;
        }
        if (end.toUpperCase().equals("A")) {
            sum = sum + 10;
        } else if(Arrays.asList(verifyCode).contains(end)){
            sum = sum + Integer.parseInt(end);
        }else {
            return false;
        }
        return sum % 11 == 0;
    }
Javascript版
function validateHKCard(id){

	var sum = 0;
	const verifyCode =[0,1,2,3,4,5,6,7,8,9]

	if(id.length == 9){
		sum = (id.substr(0,1).toUpperCase().charCodeAt()-55)*9 + (id.substr(1,1).toUpperCase().charCodeAt()-55)*8;
		id = id.substr(1,8);
	}else if(id.length == 8){
		sum = 324 + (id.substr(0,1).toUpperCase().charCodeAt()-55)*8;
	}else{
		return false;
	}
	var end = id.substr(7,1);
	var iflag = 7;
	var i;
	for(i = 1;i<=6;i++,iflag--){
		sum = sum + parseInt(id.substr(i,1))*iflag;
	}
	if(end.toUpperCase() == ("A")){
		sum = sum + 10;
	}else if(verifyCode.includes(parseInt(end))){
		sum = sum + parseInt(end);
	}else{
		return false;
	}
	return sum % 11 == 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5636991.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存