Tencent's anti-mystery ID verification algorithm. ID verification algorithm

//Top-level package

    onIdenBlur:function()
        {
            var id_type = $('#card_type').val(); //类型1为身份证验证
            var id_num =  $('#uv_aq_identity').val(); //身份证号
            id_num = $.trim(id_num);
            if(id_num == ""){
                $("#uv_aq_identity").parent().next().show().find("span").text("不能为空");
                return false;
            }
            var flag = true;
            if(id_type == "1")
            {
                if(!this.IdCardValidate(id_num)){
                    flag = false;
                }
            }
            else if(id_type == "3")
            {
                var pattern = /^(H|h|M|m)(\d{10})$/;
                if(!pattern.test(id_num)){
                    flag = false;
                }
            }
            if(!flag){
                $("#uv_aq_identity").parent().next().show().find("span").text("证件号码格式不对");
            }else{
                $("#card_type_index").val(id_type);
            }
            return flag;
        }

// The number of ID cards in the middle is 15 or 18

IdCardValidate:function(idCard)
        {
                idCard = $.trim(idCard);               //去掉字符串头尾空格  
                if (idCard.length == 15) 
                {
                    return this.isValidityBrithBy15IdCard(idCard);       //进行15位身份证的验证
                }
                else if (idCard.length == 18) 
                {
                    var a_idCard = idCard.split("");                // 得到身份证数组  
                    if(this.isValidityBrithBy18IdCard(idCard)&&this.isTrueValidateCodeBy18IdCard(a_idCard))
                    {
                        //进行18位身份证的基本验证和第18位的验证
                        return true;
                    }
                    else
                    {
                        return false; 
                    }
                }
                else
                {
                    return false;
                }
        }

//ID verification algorithm (function)

//十五位
 isValidityBrithBy15IdCard:function(idCard15)
        {
                var year =  idCard15.substring(6,8);   
                var month = idCard15.substring(8,10);   
                var day = idCard15.substring(10,12);   
                var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));   
                // 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法   
                if(temp_date.getYear()!=parseFloat(year)   
                        ||temp_date.getMonth()!=parseFloat(month)-1   
                        ||temp_date.getDate()!=parseFloat(day))
                {   
                        return false;   
                }  
                else 
                {    
                    return true;   
                }   
        }

//Eighteen

isValidityBrithBy18IdCard:function(idCard18)
        {
            var year =  idCard18.substring(6,10);   
            var month = idCard18.substring(10,12);   
            var day = idCard18.substring(12,14);   
            var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));   
                // 这里用getFullYear()获取年份,避免千年虫问题   
                if(temp_date.getFullYear()!=parseFloat(year)   
                    ||temp_date.getMonth()!=parseFloat(month)-1   
                    ||temp_date.getDate()!=parseFloat(day)){   
                    return false;   
            }else{   
                return true;   
            }    
        }

///十八位的第二个验证
 isTrueValidateCodeBy18IdCard:function(a_idCard)
        {
            var ValideCode = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ];    // 身份证验证位值.10代表X
            var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ];    // 加权因子
            var sum = 0;                             // 声明加权求和变量   
            if (a_idCard[17].toLowerCase() == 'x') {   
                a_idCard[17] = 10;                    // 将最后位为x的验证码替换为10方便后续操作   
            }       
            for ( var i = 0; i < 17; i++) {   
                sum += Wi[i] * a_idCard[i];            // 加权求和   
            }       
            valCodePosition = sum % 11;                // 得到验证码所位置   
            if (a_idCard[17] == ValideCode[valCodePosition]) 
            {       
                return true;   
            }       
            else    
            {       
                return false;   
            }       
        }

//Source https://aq.qq.com/v2/uv_aq/html/mbmanage/pc_mobile_uv_2.html
1. Non-empty judgment
2. Digit judgment
3. ID card number verification
conclusion: multiple attempts to enter wrong verification I found out that his algorithm can verify whether the ID number is filled in correctly. This algorithm only verifies the number of digits, birthday and the last digit

Guess you like

Origin blog.csdn.net/qq_35189120/article/details/83474122