js验证身份证:
function isIdCard(idCard) { var num = idCard.toLowerCase().match(/w/g); if (idCard.match(/^d{17}[dx]$/i)) { var sum = 0, times = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; for (var i = 0; i < 17; i++) { sum += parseInt(num[i], 10) * times[i]; } if ('10x98765432'.charAt(sum % 11) != num[17]) { return false; } return !!idCard.replace(/^d{6}(d{4})(d{2})(d{2}).+$/, '$1-$2-$3'); } if (idCard.match(/^d{15}$/)) { return !!idCard.replace(/^d{6}(d{2})(d{2})(d{2}).+$/, '19$1-$2-$3'); } return false; }
检查身份证是否小于 18岁:
function checkAge(UUserCard) { var myDate = new Date(); var month = myDate.getMonth() + 1; var day = myDate.getDate(); var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1; if ( UUserCard.substring(10, 12) < month || (UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) ) { age++; } return age; }