js校验身份证,—没有最好,只有更好

18位身份证校验

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function idcard(){
    var idcard = $("#idcard").val();
    var code = idcard.substring(0,17);
    var weight= [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
    var validate= ['1','0','X','9','8','7','6','5','4','3','2'];
    var arr = code.split("");
    var sum = 0;
    for(var i=0;i<17;i++){
        var cheng=Number(arr[i]) * Number(weight[i]);
        var sum = sum + cheng;
    }
    var mode = sum % 11;
    var res = code+validate[mode.toString()];
    console.log(idcard)
    console.log(res)
    if(idcard.toString() != res.toString()){
        alert("error")
    }else{
        alert("yes")
    }
}
Posted in: CSS
ˆ Back To Top