求个身份证号 跪求用JS判断身份证号的合法性

来源:互联网  时间:2016/7/25 2:23:25

关于网友提出的“求个身份证号 跪求用JS判断身份证号的合法性”问题疑问,本网通过在网上对“求个身份证号 跪求用JS判断身份证号的合法性”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题:求个身份证号 跪求用JS判断身份证号的合法性
描述:

我想让他输入身份证号,先判断输入的是否合法 (15位,18位,或17位加X)。然后根据输入的合法身份证号来 得到 出生日期 和性别 请问怎么 写JS代码 
谢谢。好的话 追加分数


解决方案1:

自己使用的JS 


function   isChinaIDCard(message,isNeedTrim)
{
    this.message  =  message;
this.isNeedTrim  =  isNeedTrim;
    
    this.validate = function(fieldObject) 
{
if (this.isNeedTrim != false)
{
setElementValue(fieldObject,trimStr(getElementValue(fieldObject)));
}
//得到fieldObject的值
var fieldStringValue = getElementValue(fieldObject);
var errorMsg ="";
fieldStringValue   =   fieldStringValue.toString()   
        if   (fieldStringValue.length==18)   
        {   
            var   a,b,c ;
            if   (!isInteger(fieldStringValue.substr(0,17)))   
            {
                errorMsg = message + "Identity card number validation error:Must be 17 before the figures";
                return errorMsg;
            }   
            a=parseInt(fieldStringValue.substr(0,1))*7+parseInt(fieldStringValue.substr(1,1))*9+parseInt(fieldStringValue.substr(2,1))*10;   
            a=a+parseInt(fieldStringValue.substr(3,1))*5+parseInt(fieldStringValue.substr(4,1))*8+parseInt(fieldStringValue.substr(5,1))*4;   
            a=a+parseInt(fieldStringValue.substr(6,1))*2+parseInt(fieldStringValue.substr(7,1))*1+parseInt(fieldStringValue.substr(8,1))*6;     
            a=a+parseInt(fieldStringValue.substr(9,1))*3+parseInt(fieldStringValue.substr(10,1))*7+parseInt(fieldStringValue.substr(11,1))*9;     
            a=a+parseInt(fieldStringValue.substr(12,1))*10+parseInt(fieldStringValue.substr(13,1))*5+parseInt(fieldStringValue.substr(14,1))*8;     
            a=a+parseInt(fieldStringValue.substr(15,1))*4+parseInt(fieldStringValue.substr(16,1))*2;   
            b=a%11;   
            
            //最后一位为校验位
            
            if   (b==2)      
            {
                //转为大写X 
                
                c=fieldStringValue.substr(17,1).toUpperCase();     
            }   
            else   
            {   
                c=parseInt(fieldStringValue.substr(17,1));   
            }   
    
            switch(b)   
            {      
                case   0:   if   (   c!=1   )   {errorMsg = message+ "Identity card number validation error: The last one should be: 1";return   errorMsg;}break;
                case   1:   if   (   c!=0   )   {errorMsg = message+ "Identity card number validation error: The last one should be: 0";return   errorMsg;}break;
                case   2:   if   (   c!="X")   {errorMsg = message+ "Identity card number validation error: The last one should be: X";return   errorMsg;}break; 
                case   3:   if   (   c!=9   )    {errorMsg = message+ "Identity card number validation error: The last one should be: 9";return   errorMsg;}break;
                case   4:   if   (   c!=8   )   {errorMsg = message+ "Identity card number validation error: The last one should be: 8";return   errorMsg;}break;
                case   5:   if   (   c!=7   )   {errorMsg = message+ "Identity card number validation error: The last one should be: 7";return   errorMsg;}break;
                case   6:   if   (   c!=6   )   {errorMsg = message+ "Identity card number validation error: The last one should be: 6";return   errorMsg;}break;
                case   7:   if   (   c!=5   )   {errorMsg = message+ "Identity card number validation error: The last one should be: 5";return   errorMsg;}break;
                case   8:   if   (   c!=4   )   {errorMsg = message+ "Identity card number validation error: The last one should be: 4";return   errorMsg;}break;
                case   9:   if   (   c!=3   )   {errorMsg = message+ "Identity card number validation error: The last one should be: 3";return   errorMsg;}break;
                case   10:   if   (   c!=2   )   {errorMsg = message+ "Identity card number validation error: The last one should be: 2";return   errorMsg;}
            }   
        }
        //15位身份证号 
        
        else     
        {
            if   (!isInteger(fieldStringValue))   
            {
                errorMsg = message + "Identity card number validation error:Before the 15 can not contain the letters!"; 
                return errorMsg;
            }     
        }   
    
        switch(fieldStringValue.length)
        {   
        
            case   15:     
                  if   (isValidDate("19"+fieldStringValue.substr(6,2),fieldStringValue.substr(8,2),fieldStringValue.substr(10,2)))   
                  {
                    return   null;
                  }   
                  else   
                  {
                    errorMsg = message + "Identity card number validation error:Date of birth wrong format!";
                    return  errorMsg;
                  }   
            case   18:     
                  if   (isValidDate(fieldStringValue.substr(6,4),fieldStringValue.substr(10,2),fieldStringValue.substr(12,2)))   
                  {
                    return   null;
                  }   
                  else   
                  {
                    errorMsg = message + "Identity card number validation error:Date of birth wrong format!";
                  }   
        } 
        errorMsg = message + "Identity card number validation error:ID card numbers must be 15 or 18!";
        return errorMsg  
    }
  }   
解决方案2:

function showBirthday(val)
  {
   var birthdayValue;
   if(15==val.length)
   { //15位身份证号码
    birthdayValue = val.charAt(6)+val.charAt(7);
    if(parseInt(birthdayValue)<10)
    {
     birthdayValue = '20'+birthdayValue;
    }
    else
    {
     birthdayValue = '19'+birthdayValue;
    }
    birthdayValue=birthdayValue+'-'+val.charAt(8)+val.charAt(9)+'-'+val.charAt(10)+val.charAt(11);
    if(parseInt(val.charAt(14)/2)*2!=val.charAt(14))
     document.all.sex.value='男';
    else
     document.all.sex.value='女';
    document.all.birthday.value=birthdayValue;
   }
   if(18==val.length)
   { //18位身份证号码
    birthdayValue=val.charAt(6)+val.charAt(7)+val.charAt(8)+val.charAt(9)+'-'+val.charAt(10)+val.charAt(11)  
   +'-'+val.charAt(12)+val.charAt(13);
    if(parseInt(val.charAt(16)/2)*2!=val.charAt(16))
     document.all.sex.value='男';
    else
     document.all.sex.value='女';
    if(val.charAt(17)!=IDCard(val))
    {
     document.all.idCard.style.backgroundColor='#ffc8c8';
    }
    else
    {
     document.all.idCard.style.backgroundColor='white';
    }
    document.all.birthday.value=birthdayValue;
   }
  }
  // 18位身份证号最后一位校验
  function IDCard(Num)
  {
   if (Num.length!=18)
    return false;
   var x=0;
   var y='';
   for(i=18;i>=2;i--)
    x = x + (square(2,(i-1))%11)*parseInt(Num.charAt(19-i-1));
   x%=11;
   y=12-x;
   if (x==0)
    y='1';
   if (x==1)
    y='0';
   if (x==2)
    y='X';
   return y;
  }
  // 求得x的y次方
  function square(x,y)
  {
   var i=1;
   for (j=1;j<=y;j++)
    i*=x;
   return i;
  }
 

解决方案3:

    var Errors=new Array(
        "验证通过!",
        "身份证号码位数不对!",
        "身份证号码出生日期超出范围或含有非法字符!",
        "身份证号码校验错误!",
        "身份证地区非法!"
    );
    var area={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"} 
    var idcard,Y,JYM;
    var S,M;
    var idcard_array = new Array();
    idcard_array = idcard.split("");
    //地区检验
    if(area[parseInt(idcard.substr(0,2))]==null) return Errors[4];
    //身份号码位数及格式检验
    switch(idcard.length){
        case 15:
        if ( (parseInt(idcard.substr(6,2))+1900) % 4 == 0 || ((parseInt(idcard.substr(6,2))+1900) % 100 == 0 && (parseInt(idcard.substr(6,2))+1900) % 4 == 0 )){
            ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//测试出生日期的合法性
        } else {
            ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//测试出生日期的合法性
        }
        if(ereg.test(idcard)){
            return Errors[0];
        }else{
            return Errors[2];
        }
        break;
        case 18:
        //18位身份号码检测
        //出生日期的合法性检查
        //闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
        //平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
        if ( parseInt(idcard.substr(6,4)) % 4 == 0 || (parseInt(idcard.substr(6,4)) % 100 == 0 && parseInt(idcard.substr(6,4))%4 == 0 )){
            ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式
        } else {
            ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正则表达式
        }
        if(ereg.test(idcard)){//测试出生日期的合法性
            //计算校验位
            S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
            + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9
            + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10
            + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5
            + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8
            + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4
            + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2
            + parseInt(idcard_array[7]) * 1
            + parseInt(idcard_array[8]) * 6
            + parseInt(idcard_array[9]) * 3 ;
            Y = S % 11;
            M = "F";
            JYM = "10X98765432";
            M = JYM.substr(Y,1);//判断校验位
            if(M == idcard_array[17]){
                return Errors[0]; //检测ID的校验位
            }else{
                return Errors[3];
            }
        }else{
            return Errors[2];
        }
        break;
        default:
        return Errors[1];
        break;
    }
}

上一篇怎么给2005的Page加其它事件,现在就一个Page_Load,2003中选中页面点属性中的闪电符号就有,2005在哪里呀?
下一篇请问OracleTypeCursor 对应到OleDbType 中是什么?
明星图片
相关文章
《求个身份证号 跪求用JS判断身份证号的合法性》由码蚁之家搜集整理于网络,
联系邮箱:mxgf168#qq.com(#改为@)