Skip to content

Instantly share code, notes, and snippets.

@NinoFocus
Last active September 6, 2017 10:50
Show Gist options
  • Save NinoFocus/37a12b671b0b24e85b69dab641630df0 to your computer and use it in GitHub Desktop.
Save NinoFocus/37a12b671b0b24e85b69dab641630df0 to your computer and use it in GitHub Desktop.
检测输入的字符串是否是新版身份证号码
const WEIGHT_MAP = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
const INSPECT_MAP = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
/**
* [isIdCard description]
* @param {String} idCard 身份证字符串
* @return {Boolean}
*/
export default function isIdCard(idCard) {
if (!idCard || typeof idCard !== 'string' || idCard.length < 18) return false
let ids = idCard.split('')
let sum = 0
for (let i = 0; i < 17; i++) {
sum += (+ids[i] * WEIGHT_MAP[i])
}
let inspect = INSPECT_MAP[sum % 11]
return inspect === ids[17]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment