身份证号码有效性检测算法 ( js版 转 .net版 )
1 #region 检测是否是正确的身份证 2 /// <summary> 3 /// 身份证验证 4 /// </summary> 5 /// <param name="num"></param> 6 /// <returns></returns> 7 public static bool isIdCardNo(string cardid) 8 { 9 string num = cardid.ToUpper(); 10 int[] factorArr = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 }; 11 char[] parityBit = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; 12 char[] varArray = new char[18]; 13 var lngProduct = 0; 14 int intCheckDigit = 0; 15 var intStrLen = num.Length; 16 var idNumber = num; 17 // initialize 18 if ((intStrLen != 15) && (intStrLen != 18)) 19 { 20 return false; 21 } 22 // check and set value 23 24 for (int i = 0; i < intStrLen; i++) 25 { 26 varArray[i] = (char)Convert.ToInt32(idNumber[i]); 27 if (((varArray[i] - 48) < 0 || (varArray[i] - 48) > 9) && (i != 17)) 28 { 29 return false; 30 } 31 else if (i < 17) 32 { 33 varArray[i] = (char)((varArray[i] - 48) * factorArr[i]); 34 } 35 } 36 if (intStrLen == 18) 37 { 38 //check date 39 var date8 = idNumber.Substring(6, 8); 40 if (isDate8(date8) == false) 41 { 42 return false; 43 } 44 // calculate the sum of the products 45 for (int i = 0; i < 17; i++) 46 { 47 lngProduct = lngProduct + varArray[i]; 48 } 49 // calculate the check digit 50 intCheckDigit = parityBit[lngProduct % 11]; 51 // check last digit 52 if (varArray[17] != intCheckDigit) 53 { 54 return false; 55 } 56 } 57 else 58 { //length is 15 59 //check date 60 var date6 = idNumber.Substring(6, 6); 61 if (isDate6(date6) == false) 62 { 63 return false; 64 } 65 } 66 return true; 67 } 68 69 /// <summary> 70 /// 15位身份证日期检测--年/月--6位 71 /// </summary> 72 /// <param name="sDate"></param> 73 /// <returns></returns> 74 static bool isDate6(string sDate) 75 { 76 if (!Regex.IsMatch(sDate, "^[0-9]{6}$")) 77 { 78 return false; 79 } 80 int year, month; 81 year = Convert.ToInt32(sDate.Substring(0, 4)); 82 month = Convert.ToInt32(sDate.Substring(4, 2)); 83 if (year < 1700 || year > 2500) return false; 84 if (month < 1 || month > 12) return false; 85 return true; 86 } 87 88 /// <summary> 89 /// 18位身份证日期检测--年/月/日--8位 90 /// </summary> 91 /// <param name="sDate"></param> 92 /// <returns></returns> 93 static bool isDate8(string sDate) 94 { 95 if (!Regex.IsMatch(sDate, "^[0-9]{8}$")) 96 { 97 return false; 98 } 99 int year, month, day; 100 year = Convert.ToInt32(sDate.Substring(0, 4)); 101 month = Convert.ToInt32(sDate.Substring(4, 2)); 102 day = Convert.ToInt32(sDate.Substring(6, 2)); 103 int[] iaMonthDays = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 104 if (year < 1700 || year > 2500) return false; 105 if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1] = 29; 106 if (month < 1 || month > 12) return false; 107 if (day < 1 || day > iaMonthDays[month - 1]) return false; 108 return true; 109 } 110 #endregion
javascript版
function isIdCardNo(num) { var factorArr = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1); var parityBit = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"); var varArray = new Array(); var intValue; var lngProduct = 0; var intCheckDigit; var intStrLen = num.length; var idNumber = num; // initialize if ((intStrLen != 15) && (intStrLen != 18)) { return false; } // check and set value for (i = 0; i < intStrLen; i++) { varArray[i] = idNumber.charAt(i); if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) { return false; } else if (i < 17) { varArray[i] = varArray[i] * factorArr[i]; } } if (intStrLen == 18) { //check date var date8 = idNumber.substring(6, 14); if (isDate8(date8) == false) { return false; } // calculate the sum of the products for (i = 0; i < 17; i++) { lngProduct = lngProduct + varArray[i]; } // calculate the check digit intCheckDigit = parityBit[lngProduct % 11]; // check last digit if (varArray[17] != intCheckDigit) { return false; } } else { //length is 15 //check date var date6 = idNumber.substring(6, 12); if (isDate6(date6) == false) { return false; } } return true; } function isDate6(sDate) { if (!/^[0-9]{6}$/.test(sDate)) { return false; } var year, month, day; year = sDate.substring(0, 4); month = sDate.substring(4, 6); if (year < 1700 || year > 2500) return false if (month < 1 || month > 12) return false return true } /** * 判断是否为“YYYYMMDD”式的时期 * */ function isDate8(sDate) { if (!/^[0-9]{8}$/.test(sDate)) { return false; } var year, month, day; year = sDate.substring(0, 4); month = sDate.substring(4, 6); day = sDate.substring(6, 8); var iaMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if (year < 1700 || year > 2500) return false if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1] = 29; if (month < 1 || month > 12) return false if (day < 1 || day > iaMonthDays[month - 1]) return false return true }
!!!转发请注明出处和作者名称。
!!!转发请注明出处和作者名称。
!!!转发请注明出处和作者名称。
重要的事,说三遍。