/*身份證 驗證*/ function checkIDCard(idcode) { if (idcode != "" && idcode != null && idcode != "undefined" && (idcode.length == 15 || idcode.length == 18)) { // 加權因子 var weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; // 校驗碼 var check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']; var code = idcode + ""; var last = idcode[17];//最後一個 var seventeen = code.substring(0, 17); // ISO 7064:1983.MOD 11-2 // 判斷最後一位校驗碼是否正確 var arr = seventeen.split(""); var len = arr.length; var num = 0; for (var i = 0; i < len; i++) { num = num + arr[i] * weight_factor[i]; } // 獲取餘數 var resisue = num % 11; var last_no = check_code[resisue]; /* 1、第一位不可能是0 2、第二位到第六位可以是0-9 3、第七位到第十位是年份,所以七八位爲19或者20 4、十一位和十二位是月份,這兩位是01-12之間的數值 5、十三位和十四位是日期,是從01-31之間的數值 6、十五,十六,十七都是數字0-9 7、十八位可能是數字0-9,也可能是X */ var idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/; // 判斷格式是否正確 var format = idcard_patter.test(idcode); // 返回驗證結果,校驗碼和格式同時正確纔算是合法的身份證號碼 return last === last_no && format ? true : false; } return false; } /*身份證獲取(出生年月,性別)*/ $("#member-info-tbody").on("input propertychange", "input[name='cardNumber']", function () { var that = $(this); var $thisVal = $.trim($(this).val()); if ($thisVal != "" && $thisVal != null && $thisVal != "undefined" && ($thisVal.length == 15 || $thisVal.length == 18)) { var checkResult = checkIDCard($thisVal);//判斷身份證號是否正確 if (checkResult) { var dateLabel = "";//出生年月 if ($thisVal.length == 15) { dateLabel = '19' + $thisVal.substr(6, 2) + '-' + $thisVal.substr(8, 2) + '-' + $thisVal.substr(10, 2); } else if ($thisVal.length == 18) { dateLabel = $thisVal.substr(6, 4) + '-' + $thisVal.substr(10, 2) + '-' + $thisVal.substr(12, 2); } $(that).parent().parent().find("input[name='birthday']").val(dateLabel); var sexNo = "";//性別 if ($thisVal.length == 15) { sexNo = $thisVal.substring(14, 15); } else if ($thisVal.length == 18) { sexNo = $thisVal.substring(16, 17); } var sexLabel = sexNo % 2; if (sexLabel == 0) { sexLabel = '女'; } else { sexLabel = '男'; } $(that).parent().parent().find("input:checkbox[name='sex']").prop('checked', false); $(that).parent().parent().find("input:checkbox[name='sex'][value='" + sexLabel + "']").prop('checked', true); } } });
身份證號判斷男女和生日
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.